forked from Monemax94/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
48 lines (44 loc) · 1.05 KB
/
_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
#include "main.h"
/**
* _printf - produces output according to a format
* @format: format string containing the characters and the specifiers
* Description: this function will call the get_print() function that will
* determine which printing function to call depending on the conversion
* specifiers contained into fmt
* Return: length of the formatted output string
*/
int _printf(const char *format, ...)
{
int (*pfunc)(va_list, flags_t *);
const char *p;
va_list arguments;
flags_t flags = {0, 0, 0};
register int count = 0;
va_start(arguments, format);
if (!format || (format[0] == '%' && !format[1]))
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (-1);
for (p = format; *p; p++)
{
if (*p == '%')
{
p++;
if (*p == '%')
{
count += _putchar('%');
continue;
}
while (get_flag(*p, &flags))
p++;
pfunc = get_print(*p);
count += (pfunc)
? pfunc(arguments, &flags)
: _printf("%%%c", *p);
} else
count += _putchar(*p);
}
_putchar(-1);
va_end(arguments);
return (count);
}