-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs2.c
204 lines (170 loc) · 4.18 KB
/
funcs2.c
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
#include "main.h"
/**
* for_reverse - Prints reverse string.
* @types: Lista of arguments
* @buff: Buffer array to handle print
* @flags: Calculates active flags
* @width: get width
* @precision: Precision specification
* @size: Size specifier
* @args: Arguments
* Return: Numbers of chars printed
*/
int for_reverse(va_list args, char buff[],
int flags, int width, int precision, int size)
{
char *str;
int i, p = 0;
UNUSED(buff);
UNUSED(flags);
UNUSED(width);
UNUSED(size);
str = va_arg(args, char *);
if (str == NULL)
{
UNUSED(precision);
str = ")Null(";
}
for (i = 0; str[i]; i++)
for (i = i - 1; i >= 0; i--)
{
char z = str[i];
write(1, &z, 1);
p++;
}
return (p);
}
/**
* for_pointer - Prints the value of a pointer variable
* @types: List a of arguments
* @buff: Buffer array to handle print
* @flags: Calculates active flags
* @width: get width
* @precision: Precision specification
* @size: Size specifier
* @args: Arguments
* Return: Number of chars printed.
*/
int for_pointer(va_list args, char buff[],
int flags, int width, int precision, int size)
{
char Qir = 0, padd = ' ';
int index = BUFF_SIZE - 2, length = 2, padd_start = 1;
unsigned long rest_d;
char contain_er[] = "0123456789abcdef";
void *addrs = va_arg(args, void *);
UNUSED(width);
UNUSED(size);
if (addrs == NULL)
return (write(1, "(nil)", 5));
buff[BUFF_SIZE - 1] = '\0';
UNUSED(precision);
rest_d = (unsigned long)addrs;
while (rest_d > 0)
{
buff[index--] = contain_er[rest_d % 16];
rest_d /= 16;
length++;
}
if ((flags & F_ZERO) && !(flags & F_MINUS))
padd = '0';
if (flags & F_PLUS)
Qir = '+', length++;
else if (flags & F_SPACE)
Qir = ' ', length++;
index++;
/*return (write(1, &buff[i], BUFF_SIZE - i - 1));*/
return (do_pointer(buff, index, length,
width, flags, padd, Qir, padd_start));
}
/**
* for_hex - Prints a hexadecimal number in lower or upper
* @types: Lista of arguments
* @map_to: Array of values to map the number to
* @buff: Buffer array to handle print
* @flags: Calculates active flags
* @flag_ch: Calculates active flags
* @width: get width
* @precision: Precision specification
* @size: Size specifier
* @size: Size specification
* @args: Arguments
* @contain_er: Container
* Return: Number of chars printed
*/
int for_hex(va_list args, char contain_er[], char buff[],
int flags, char lol, int width, int precision, int size)
{
int i = BUFF_SIZE - 2;
unsigned long int number;
unsigned long int all_num;
number = va_arg(args, unsigned long int);
all_num = number;
UNUSED(width);
number = turn_unsgnd(number, size);
if (number == 0)
buff[i--] = '0';
buff[BUFF_SIZE - 1] = '\0';
while (number > 0)
{
buff[i--] = contain_er[number % 16];
number /= 16;
}
if (flags & F_HASH && all_num != 0)
{
buff[i--] = lol;
buff[i--] = '0';
}
i++;
return (do_unsgnd(0, i, buff, flags, width, precision, size));
}
/**
* for_nonprintable - Prints ascii codes in hexa of non printable chars
* @types: Lista of arguments
* @buff: Buffer array to handle print
* @flags: Calculates active flags
* @width: get width
* @precision: Precision specification
* @size: Size specifier
* @args: Arguments
* Return: Number of chars printed
*/
int for_nonprintable(va_list args, char buff[],
int flags, int width, int precision, int size)
{
int course = 0;
int i = 0;
char *str = va_arg(args, char *);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
if (str == NULL)
return (write(1, "(null)", 6));
while (str[i] != '\0')
{
if (is_printable(str[i]))
buff[i + course] = str[i];
else
course += add_hexa_code(str[i], buff, i + course);
i++;
}
buff[i + course] = '\0';
return (write(1, buff, i + course));
}
/**
* for_hex_upper - Prints an unsigned number in upper hexadecimal notation
* @args: Lista of arguments
* @buff: Buffer array to handle print
* @flags: Calculates active flags
* @width: get width
* @precision: Precision specification
* @size: Size specifier
* Return: Number of chars printed
*/
int for_hex_upper(va_list args, char buff[],
int flags, int width, int precision, int size)
{
return (for_hex(args, "0123456789ABCDEF", buff,
flags, 'X', width, precision, size));
}