-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
49 lines (46 loc) · 978 Bytes
/
_printf.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
#include "main.h"
/**
* _printf - prints anything
* @format: the format string
*
* Return: number of bytes printed
*/
int _printf(const char *format, ...)
{
int sum = 0;
va_list ap;
char *p, *start;
params_t params = PARAMS_INIT;
va_start(ap, format);
if (!format || (format[0] == '%' && !format[1]))
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (-1);
for (p = (char *)format; *p; p++)
{
init_params(¶ms, ap);
if (*p != '%')
{
sum += _putchar(*p);
continue;
}
start = p;
p++;
while (get_flag(p, ¶ms)) /* while char at p is flag char */
{
p++; /* next char */
}
p = get_width(p, ¶ms, ap);
p = get_precision(p, ¶ms, ap);
if (get_modifier(p, ¶ms))
p++;
if (!get_specifier(p))
sum += print_from_to(start, p,
params.l_modifier || params.h_modifier ? p - 1 : 0);
else
sum += get_print_func(p, ap, ¶ms);
}
_putchar(BUF_FLUSH);
va_end(ap);
return (sum);
}