-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
executable file
·75 lines (71 loc) · 1.13 KB
/
_printf.c
File metadata and controls
executable file
·75 lines (71 loc) · 1.13 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
#include "main.h"
/**
* check_flags - checks flags
* @f: flag
* @f2: format
*
* Return: void
*/
int check_flags(char f, char f2)
{
if (f == '+')
{
_putchar('+');
return (1);
}
if (f == ' ')
{
_putchar(' ');
return (1);
}
if (f == '#')
{
_putchar('0');
if (f2 == 'x')
_putchar('x');
if (f2 == 'X')
_putchar('X');
return (1);
}
return (0);
}
/**
* _printf - a variadic function that prints formatted strings.
* @format: input string
*
* Return: number of chars printed
*/
int _printf(const char *format, ...)
{
va_list ap;
int i, chars = 0, new_printed = 0;
/* process the string normally except char % with s, c, d, r, R, or i */
if (format == NULL)
return (-1);
va_start(ap, format);
for (i = 0; format[i] != '\0'; i++)
{
if (format[i] != '%')
{
write(1, &format[i], 1);
chars++;
}
else
{
new_printed = 0;
i++;
if (check_flags(format[i], format[i + 1]))
{
new_printed++;
i++;
}
new_printed = choose_f(format[i], ap);
/* Negative error in failures */
if (new_printed < 0)
return (-1);
chars += new_printed;
}
}
va_end(ap);
return (chars);
}