-
Notifications
You must be signed in to change notification settings - Fork 0
/
printf_funcs.c
197 lines (182 loc) · 4.61 KB
/
printf_funcs.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
#include "main.h"
/**
* char_print - character printing
* @flag: flag characters for non-custom conversion specifiers
* @args: variable argument list
* @count: pointer to integer to store the count of characters printed
* @space: space character for non-custom conversion specifiers
* c - int parameter to print promoted from char
* char is promoted to int when passed to the elipses
*/
void char_print(char __attribute__ ((unused)) flag, va_list args,
int *count, char __attribute__ ((unused)) space)
{
char c;
c = va_arg(args, int);
_putchar(c);
(*count)++;
}
/**
* string_print - string printing function
* @flag: flag characters for non-custom conversion specifiers
* @args: variable argument list
* @count: pointer to integer to store the count of characters printed
* @space: space character for non-custom conversion specifiers
* s - string (char *) parameter to print
*/
void string_print(char __attribute__ ((unused)) flag, va_list args,
int *count, char __attribute__ ((unused)) space)
{
char *s;
int i;
s = va_arg(args, char *);
if (s == NULL)
{
s = "(null)";
}
for (i = 0; s[i] != '\0'; i++)
{
_putchar(s[i]);
(*count)++;
}
}
/**
* bin_print - decimal (base 2) printing function
* @flag: flag characters for non-custom conversion specifiers
* @args: variable argument list
* @count: pointer to integer to store the count of characters printed
* @space: space character for non-custom conversion specifiers
* b - binary (unsigned int) parameter passed
*/
void bin_print(char __attribute__ ((unused)) flag, va_list args,
int *count, char __attribute__ ((unused)) space)
{
unsigned int b, i, b_len, num;
int j;
char *digits;
b = va_arg(args, unsigned int);
num = b; /* hold the value of b before passing it to count logic */
/* count number of digits */
b_len = 0;
while (b / 2 != 0)
{
b_len++;
b = b / 2;
}
b_len++;
/* dynamically allocate memory for digits array */
digits = malloc(sizeof(char) * (b_len + 1));
if (digits == NULL)
{
return;
}
/* convert integer to string of binary digits */
i = 0;
while (num / 2 != 0)
{
digits[i] = (num % 2) + '0';
num = num / 2;
i++;
}
digits[i] = (num % 2) + '0';
/* print binary digits in reverse order */
for (j = i; j >= 0; j--)
{
_putchar(digits[j]);
(*count)++;
}
free(digits);
}
/**
* oct_print - unsigned integer printing function in octal format (base 8)
* @flag: flag characters for non-custom conversion specifiers
* @args: variable argument list
* @count: pointer to integer to store the count of characters printed
* @space: space character for non-custom conversion specifiers
* o - unsigned integer (unsigned int) parameter to print in octal format
*/
void oct_print(char flag, va_list args,
int *count, char __attribute__ ((unused)) space)
{
unsigned int o, i, o_len, num;
int j;
char *digits;
o = va_arg(args, unsigned int);
num = o; /* hold the value of u b4 passing it to count */
/* count number of digits */
o_len = 0;
while (o / 8 != 0)
{
o_len++;
o = o / 8;
}
o_len++;
/* dynamically allocate memory for digits array */
digits = malloc(sizeof(char) * (o_len + 1));
if (digits == NULL)
return;
/* convert unsigned int to str of digits in octal fmt */
i = 0;
while (num / 8 != 0)
{
digits[i] = (num % 8) + '0';
num = num / 8;
i++;
}
digits[i] = (num % 8) + '0';
if (flag == '#' && num != 0)
{ _putchar('0');
(*count)++; }
/* print digits in reverse order */
for (j = i; j >= 0; j--)
{
_putchar(digits[j]);
(*count)++;
}
free(digits);
}
/**
* uint_print - unsigned integer printing function
* @flag: flag characters for non-custom conversion specifiers
* @args: variable argument list
* @count: pointer to integer to store the count of characters printed
* @space: space character for non-custom conversion specifiers
* u - unsigned integer (unsigned int) parameter to print
*/
void uint_print(char __attribute__ ((unused)) flag, va_list args,
int *count, char __attribute__ ((unused)) space)
{
unsigned int u, i, u_len, num;
int j;
char *digits;
u = va_arg(args, unsigned int);
num = u; /* hold the value of u before passing it to count logic */
/* count number of digits */
u_len = 0;
while (u / 10 != 0)
{
u_len++;
u = u / 10;
}
u_len++;
/* dynamically allocate memory for digits array */
digits = malloc(sizeof(char) * (u_len + 1));
if (digits == NULL)
return;
/* convert unsigned integer to string of digits */
i = 0;
while (num / 10 != 0)
{
digits[i] = (num % 10) + '0';
num = num / 10;
i++;
}
digits[i] = (num % 10) + '0';
/* print digits in reverse order */
for (j = i; j >= 0; j--)
{
_putchar(digits[j]);
(*count)++;
}
free(digits);
}