-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_0.c
More file actions
188 lines (168 loc) · 3.8 KB
/
function_0.c
File metadata and controls
188 lines (168 loc) · 3.8 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
#include "main.h"
/* PRINT CHARACTER */
/**
* char_print - Function printing a character
* @type: argument list
* @buff: An array handling printing
* @flag: variable calculating flags that are active
* @width: Width
* @prec: specifies precision
* @size: specifies size
* Return: size of characters to print
*/
int char_print(va_list type, char buff[],
int flag, int width, int prec, int size)
{
char ch = va_arg(type, int);
return (handle_write_char(ch, buff, flag, width, prec, size));
}
/* PRINT A STRING */
/**
* string_print - Function printing a string
* @type: argument list
* @buff: An array handling printing
* @flag: variable calculating flags that are active
* @width: variable to get width.
* @prec: specifies precision
* @size: specifies size
* Return: size of characters to print
*/
int string_print(va_list type, char buff[],
int flag, int width, int prec, int size)
{
int i, len = 0;
char *str = va_arg(type, char *);
UNUSED(buff);
UNUSED(width);
UNUSED(flag);
UNUSED(size);
UNUSED(prec);
if (str == NULL)
{
str = "(null)";
if (prec >= 6)
str = " ";
}
while (str[len] != '\0')
len++;
if (prec >= 0 && prec < len)
len = prec;
if (width > len)
{
if (flag & FLAG_MINUS)
{
write(1, &str[0], len);
for (i = width - len; i > 0; i--)
write(1, " ", 1);
return (width);
}
else
{
for (i = width - len; i > 0; i--)
write(1, " ", 1);
write(1, &str[0], len);
return (width);
}
}
return (write(1, str, len));
}
/* PRINTING A PERCENT SIGN */
/**
* percent_print - Function printing a string
* @type: argument list
* @buffer: An array handling printing
* @flags: variable calculating flags that are active
* @width: variable to get width.
* @precision: specifies precision
* @size: specifies size
* Return: size of characters to print
*/
int percent_print(va_list type, char buffer[],
int flags, int width, int precision, int size)
{
UNUSED(type);
UNUSED(buffer);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
return (write(1, "%%", 1));
}
/* PRINTING AN INTEGER */
/**
* int_print - Prints an int
* @type: argument list
* @buffer: An array handling printing
* @flags: variable calculating flags that are active
* @width: variable to get width.
* @precision: specifies precision
* @size: specifies size
* Return: size of characters to print
*/
int int_print(va_list type, char buffer[],
int flags, int width, int precision, int size)
{
int n = BUFF_SIZE - 2;
int is_negative = 0;
long int x = va_arg(type, long int);
unsigned long int number;
x = convert_size_number(x, size);
if (x == 0)
buffer[n--] = '0';
buffer[BUFF_SIZE - 1] = '\0';
number = (unsigned long int)x;
if (x < 0)
{
number = (unsigned long int)((-1) * x);
is_negative = 1;
}
while (number > 0)
{
buffer[n--] = (number % 10) + '0';
number /= 10;
}
n++;
return (write_number(is_negative, n, buffer, flags, width, precision, size));
}
/* PRINTING A BINARY */
/**
* binary_print - Function Printing an unsigned number
* @type: argument list
* @buffer: An array handling printing
* @flags: variable calculating flags that are active
* @width: variable to get width.
* @precision: specifies precision
* @size: specifies size
* Return: size of characters to print
*/
int binary_print(va_list type, char buffer[],
int flags, int width, int precision, int size)
{
unsigned int i, j, k, sum;
unsigned int arr[32];
int count;
UNUSED(buffer);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
j = va_arg(type, unsigned int);
k = 2147483648; /* (2 ^ 31) */
arr[0] = j / k;
for (i = 1; i < 32; i++)
{
k /= 2;
arr[i] = (j / k) % 2;
}
for (i = 0, sum = 0, count = 0; i < 32; i++)
{
sum += arr[i];
if (sum || i == 31)
{
char c = '0' + arr[i];
write(1, &c, 1);
count++;
}
}
return (count);
}