-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path1-printf.c
51 lines (45 loc) · 1.05 KB
/
1-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
50
51
#include "main.h"
/**
* _printf - prints and input into the standard output
* @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]))/* checking for NULL char */
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (-1);
for (p = (char *)format; *p; p++)
{
init_params(¶ms, ap);
if (*p != '%')/*checking for the % specifier*/
{
sum += _putchar(*p);
continue;
}
start = p;
p++;
while (get_flag(p, ¶ms)) /* while char at p is flag character */
{
p++; /* next character */
}
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);
}