-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
182 lines (171 loc) · 3.58 KB
/
functions.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
#include "main.h"
/**
* output_char - outs a char
* @types: List a of arguments
* @buffer: Buffer array to handle print
* @flags: cal active flags
* @width: Width
* @precision: Precision
* @size: Size
* Return: Number of chars printed
*/
int output_char(va_list types, char buffer[],
int flags, int width, int precision, int size)
{
char ch = va_arg(types, int);
return (handle_write_char(ch, buffer, flags, width, precision,
size));
}
/**
* output_string - it Prints a string
* @types: List a of arguments
* @buffer: Buffer array to handle print
* @flags: cal active flags
* @width: get width.
* @precision: Precision
* @size: Size
* Return: no of chars printed
*/
int output_string(va_list types, char buffer[],
int flags, int width, int precision, int size)
{
int leng = 0, il;
char *strg = va_arg(types, char *);
UNUSED(precision);
UNUSED(size);
UNUSED(buffer);
UNUSED(width);
UNUSED(flags);
if (strg == NULL)
{
strg = "(null)";
if (precision >= 6)
strg = " ";
}
while (strg[leng] != '\0')
leng++;
if (precision >= 0 && precision < leng)
leng = precision;
if (width > leng)
{
if (flags & F_MINUS)
{
write(1, &strg[0], leng);
for (il = width - leng; il > 0; il--)
write(1, " ", 1);
return (width);
}
else
{
for (il = width - leng; il > 0; il--)
write(1, " ", 1);
write(1, &strg[0], leng);
return (width);
}
}
return (write(1, strg, leng));
}
/**
* output_percent - it Prints a percent sign
* @types: Lista of arguments
* @buffer: Buffer array to handle print
* @flags: cal active flags
* @width: gets width.
* @precision: Precision
* @size: Size
*
* Return: no of chars printed
*/
int output_percent(va_list types, char buffer[],
int flags, int width, int precision, int size)
{
UNUSED(types);
UNUSED(buffer);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
return (write(1, "%%", 1));
}
/**
* output_int - it Print int
* @types: Lista of arguments
* @buffer: Buffer array to handle print
* @flags: cal active flags
* @width: get width.
* @precision: Precision
* @size: Size
*
* Return: Number of chars printed
*/
int output_int(va_list types, char buffer[],
int flags, int width, int precision, int size)
{
int il = BUFF_SIZE - 2;
int is_negative = 0;
long int no = va_arg(types, long int);
unsigned long int a_num;
no = convert_size_number(no, size);
if (no == 0)
buffer[il--] = '0';
buffer[BUFF_SIZE - 1] = '\0';
a_num = (unsigned long int)no;
if (no < 0)
{
a_num = (unsigned long int)((-1) * no);
is_negative = 1;
}
while (a_num > 0)
{
buffer[il--] = (a_num % 10) + '0';
a_num /= 10;
}
il++;
return (write_number(is_negative, il, buffer, flags, width,
precision, size));
}
/**
* output_binary - Prints an unsigned number
* @types: Arguments listings
* @buffer: Buffer array for print handling
* @flags: Active flags calculator
* @width: This gets the width
* @precision: This specifies precision
* @size: This specifies size
*
* Return: Numbers of char printed.
*/
int output_binary(va_list types, char buffer[],
int flags, int width, int precision, int size)
{
unsigned int n;
unsigned int il;
unsigned int em;
unsigned int sums;
unsigned int b[32];
int count;
UNUSED(buffer);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
n = va_arg(types, unsigned int);
em = 2147483648U;
b[0] = n / em;
for (il = 1; il < 32; il++)
{
em /= 2;
b[il] = (n / em) % 2;
}
for (il = 0, sums = 0, count = 0; il < 32; il++)
{
sums += b[il];
if (sums || il == 31)
{
char z = '0' + b[il];
write(1, &z, 1);
count++;
}
}
return (count);
}