-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-specifiers.c
More file actions
227 lines (203 loc) · 5.33 KB
/
Copy path1-specifiers.c
File metadata and controls
227 lines (203 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "main.h"
/**
* print_unsigned - A function that prints an unsigned int as decimal.
*
* @data: A pointer to an arg_data_t struct.
*
* This function retrieves an unsigned int from the va_list in the arg_data_t
* struct, converts it to a string, prints it, increments the count of printed
* characters in the struct, and returns the count.
*
* Return: The total count of printed characters after the function call.
*/
int print_unsigned(arg_data_t *data)
{
unsigned int num = va_arg(data->args, unsigned int);
char buffer[11]; /* Buffer to hold the string repres. of the number */
char *str = &buffer[10]; /* Start from the end of the buffer */
int count = 0;
*str = '\0'; /* Null-terminate the string */
/* Convert the number to a string */
do {
str--;
*str = '0' + (num % 10);
num /= 10;
count++;
} while (num != 0);
if (data->flags & 2) /* if the 'space' flag is set */
{
str--;
*str = ' ';
count++;
}
/* Print the string and update the count */
while (*str)
{
_putchar(*str, data);
str++;
}
data->count += count;
return (data->count);
}
/**
* print_octal - A function that prints an unsigned int as octal.
*
* @data: A pointer to an arg_data_t struct.
*
* This function retrieves an unsigned int from the va_list in the arg_data_t
* struct, converts it to an octal string, prints it, increments the count of
* printed characters in the struct, and returns the count.
*
* Return: The total count of printed characters after the function call.
*/
int print_octal(arg_data_t *data)
{
unsigned int num = va_arg(data->args, unsigned int);
unsigned int og_num = num; /* store the original number*/
char buffer[12]; /* Buffer to hold the octal repres. of the number */
char *str = &buffer[11]; /* Start from the end of the buffer */
int count = 0;
*str = '\0'; /* Null-terminate the string */
/* Convert the number to octal */
do {
str--;
*str = '0' + (num % 8);
num /= 8;
count++;
} while (num != 0);
if (og_num != 0 && (data->flags & 4)) /* if the '#' flag is set */
{
str--;
*str = '0';
count++;
}
/* Print the octal representation and update the count */
while (*str)
{
_putchar(*str, data);
str++;
}
data->count += count;
return (data->count);
}
/**
* print_hex_lower - A function that prints an unsigned int as hexadecimal
* (lowercase).
*
* @data: A pointer to an arg_data_t struct.
*
* This function retrieves an unsigned int from the va_list in the arg_data_t
* struct, converts it to a lowercase hexadecimal string, prints it, increments
* the count of printed characters in the struct, and returns the count.
*
* Return: The total count of printed characters after the function call.
*/
int print_hex_lower(arg_data_t *data)
{
unsigned int num = va_arg(data->args, unsigned int);
unsigned int og_num = num; /* store the original number */
char buffer[9]; /* Buffer to hold the hexadecimal repres. of the number */
char *str = &buffer[8]; /* Start from the end of the buffer */
int count = 0;
*str = '\0'; /* Null-terminate the string */
/* Convert the number to hexadecimal */
do {
str--;
*str = "0123456789abcdef"[num % 16];
num /= 16;
count++;
} while (num != 0);
if (og_num != 0 && (data->flags & 4)) /* if the '#' flag is set */
{
str -= 2;
*str = '0';
*(str + 1) = 'x';
count += 2;
}
/* Print the hexadecimal representation and update the count */
while (*str)
{
_putchar(*str, data);
str++;
}
data->count += count;
return (data->count);
}
/**
* print_hex_upper - A function that prints an unsigned int as hexadecimal
* (uppercase).
*
* @data: A pointer to an arg_data_t struct.
*
* This function retrieves an unsigned int from the va_list in the arg_data_t
* struct, converts it 2 an uppercase hexadecimal string, prints it, increments
* the count of printed characters in the struct, and returns the count.
*
* Return: The total count of printed characters after the function call.
*/
int print_hex_upper(arg_data_t *data)
{
unsigned int num = va_arg(data->args, unsigned int);
unsigned int og_num = num; /* store the original number */
char buffer[9]; /* Buffer to hold the hexadecimal repres. of the number */
char *str = &buffer[8]; /* Start from the end of the buffer */
int count = 0;
*str = '\0'; /* Null-terminate the string */
/* Convert the number to hexadecimal */
do {
str--;
*str = "0123456789ABCDEF"[num % 16];
num /= 16;
count++;
} while (num != 0);
if (og_num != 0 && (data->flags & 4)) /* if the '#' flag is set */
{
str -= 2;
*str = '0';
*(str + 1) = 'X';
count += 2;
}
/* Print the hexadecimal representation and update the count */
while (*str)
{
_putchar(*str, data);
str++;
}
data->count += count;
return (data->count);
}
/**
* print_string_S - Prints a string with special characters displayed in
* hexadecimal.
* @data: A pointer to an arg_data_t structure.
*
* Return: The total count of characters printed.
*/
int print_string_S(arg_data_t *data)
{
char *str = va_arg(data->args, char *);
int count = 0;
if (str == NULL)
{
str = "(null)";
}
while (*str)
{
if (*str < 32 || *str >= 127)
{
_putchar('\\', data);
_putchar('x', data);
_putchar("0123456789ABCDEF"[(*str >> 4) & 0xF], data);
_putchar("0123456789ABCDEF"[*str & 0xF], data);
count += 4;
}
else
{
_putchar(*str, data);
count++;
}
str++;
}
data->count += count;
return (data->count);
}