-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_handle.c
More file actions
249 lines (224 loc) · 4.84 KB
/
Copy pathwrite_handle.c
File metadata and controls
249 lines (224 loc) · 4.84 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "main.h"
/**
* handle_write_char - Prints a string
* @c: char types.
* @buf: buf array to handle print
* @fl: Calculates active fl.
* @wi: get wi.
* @pre: pre specifier
* @s: s specifier
*
* Return: Number of chars printed.
*/
int handle_write_char(char c, char buf[],
int fl, int wi, int pre, int s)
{
int i = 0;
char padd = ' ';
UNUSED(pre);
UNUSED(s);
if (fl & F_ZERO)
padd = '0';
buf[i++] = c;
buf[i] = '\0';
if (wi > 1)
{
buf[BUFF_SIZE - 1] = '\0';
for (i = 0; i < wi - 1; i++)
buf[BUFF_SIZE - i - 2] = padd;
if (fl & F_MINUS)
return (write(1, &buf[0], 1) +
write(1, &buf[BUFF_SIZE - i - 1], wi - 1));
else
return (write(1, &buf[BUFF_SIZE - i - 1], wi - 1) +
write(1, &buf[0], 1));
}
return (write(1, &buf[0], 1));
}
/**
* write_number - Prints a string
* @is_negative: Lista of arguments
* @ind: char types.
* @buf: buf array to handle print
* @fl: Calculates active fl
* @wi: get wi.
* @pre: pre specifier
* @s: s specifier
*
* Return: Number of chars printed.
*/
int write_number(int is_negative, int ind, char buf[],
int fl, int wi, int pre, int s)
{
int length = BUFF_SIZE - ind - 1;
char padd = ' ', extra_ch = 0;
UNUSED(s);
if ((fl & F_ZERO) && !(fl & F_MINUS))
padd = '0';
if (is_negative)
extra_ch = '-';
else if (fl & F_PLUS)
extra_ch = '+';
else if (fl & F_SPACE)
extra_ch = ' ';
return (write_num(ind, buf, fl, wi, pre,
length, padd, extra_ch));
}
/**
* write_num - Write a number using a bufffer
* @ind: Index at which the number starts on the buf
* @buf: buf
* @fl: fl
* @wi: wi
* @prec: pre specifier
* @length: Number length
* @padd: Pading char
* @extra_c: Extra char
*
* Return: Number of printed chars.
*/
int write_num(int ind, char buf[],
int fl, int wi, int prec,
int length, char padd, char extra_c)
{
int i, padd_start = 1;
if (prec == 0 && ind == BUFF_SIZE - 2 && buf[ind] == '0' && wi == 0)
return (0);
if (prec == 0 && ind == BUFF_SIZE - 2 && buf[ind] == '0')
buf[ind] = padd = ' ';
if (prec > 0 && prec < length)
padd = ' ';
while (prec > length)
buf[--ind] = '0', length++;
if (extra_c != 0)
length++;
if (wi > length)
{
for (i = 1; i < wi - length + 1; i++)
buf[i] = padd;
buf[i] = '\0';
if (fl & F_MINUS && padd == ' ')
{
if (extra_c)
buf[--ind] = extra_c;
return (write(1, &buf[ind], length) + write(1, &buf[1], i - 1));
}
else if (!(fl & F_MINUS) && padd == ' ')
{
if (extra_c)
buf[--ind] = extra_c;
return (write(1, &buf[1], i - 1) + write(1, &buf[ind], length));
}
else if (!(fl & F_MINUS) && padd == '0')
{
if (extra_c)
buf[--padd_start] = extra_c;
return (write(1, &buf[padd_start], i - padd_start) +
write(1, &buf[ind], length - (1 - padd_start)));
}
}
if (extra_c)
buf[--ind] = extra_c;
return (write(1, &buf[ind], length));
}
/**
* write_unsgnd - Writes an unsigned number
* @is_negative: Number indicating if the num is negative
* @ind: Index at which the number starts in the buf
* @buf: Array of chars
* @fl: fl specifiers
* @wi: wi specifier
* @pre: pre specifier
* @s: s specifier
*
* Return: Number of written chars.
*/
int write_unsgnd(int is_negative, int ind,
char buf[],
int fl, int wi, int pre, int s)
{
int length = BUFF_SIZE - ind - 1, i = 0;
char padd = ' ';
UNUSED(is_negative);
UNUSED(s);
if (pre == 0 && ind == BUFF_SIZE - 2 && buf[ind] == '0')
return (0);
if (pre > 0 && pre < length)
padd = ' ';
while (pre > length)
{
buf[--ind] = '0';
length++;
}
if ((fl & F_ZERO) && !(fl & F_MINUS))
padd = '0';
if (wi > length)
{
for (i = 0; i < wi - length; i++)
buf[i] = padd;
buf[i] = '\0';
if (fl & F_MINUS)
{
return (write(1, &buf[ind], length) + write(1, &buf[0], i));
}
else
{
return (write(1, &buf[0], i) + write(1, &buf[ind], length));
}
}
return (write(1, &buf[ind], length));
}
/**
* write_pointer - Write a memory address
* @buf: Arrays of chars
* @ind: Index at which the number starts in the buf
* @length: Length of number
* @wi: Wwi specifier
* @fl: fl specifier
* @padd: Char representing the padding
* @extra_c: Char representing extra char
* @padd_start: Index at which padding should start
*
* Return: Number of written chars.
*/
int write_pointer(char buf[], int ind, int length,
int wi, int fl, char padd, char extra_c, int padd_start)
{
int i;
if (wi > length)
{
for (i = 3; i < wi - length + 3; i++)
buf[i] = padd;
buf[i] = '\0';
if (fl & F_MINUS && padd == ' ')
{
buf[--ind] = 'x';
buf[--ind] = '0';
if (extra_c)
buf[--ind] = extra_c;
return (write(1, &buf[ind], length) + write(1, &buf[3], i - 3));
}
else if (!(fl & F_MINUS) && padd == ' ')
{
buf[--ind] = 'x';
buf[--ind] = '0';
if (extra_c)
buf[--ind] = extra_c;
return (write(1, &buf[3], i - 3) + write(1, &buf[ind], length));
}
else if (!(fl & F_MINUS) && padd == '0')
{
if (extra_c)
buf[--padd_start] = extra_c;
buf[1] = '0';
buf[2] = 'x';
return (write(1, &buf[padd_start], i - padd_start) +
write(1, &buf[ind], length - (1 - padd_start) - 2));
}
}
buf[--ind] = 'x';
buf[--ind] = '0';
if (extra_c)
buf[--ind] = extra_c;
return (write(1, &buf[ind], BUFF_SIZE - ind - 1));
}