-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
90 lines (85 loc) · 1.48 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
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
#include "main.h"
/**
* *check_for_specifiers - gives count of associated argument
* _printf - print variable arguments.
* @format: Format by specifier.
* Return: count of chars.
*/
static int (*check_for_specifiers(const char *format))(va_list)
{
unsigned int i;
inputs in[] = {
{"c", pchar},
{"s", pstring},
{"i", pinti},
{"d", pintd},
{"b", pbinary},
{"S", pString},
{"p", ppoint},
{"x", pLower},
{"X", pUpper},
{"%", pmod},
{"u", punsign},
{"o", poct},
{"\0", NULL}
};
for (i = 0; in[i].fm != NULL; i++)
{
if (*(in[i].fm) == *format)
{
break;
}
}
return (in[i].fn);
}
/**
* _printf - print variable arguments.
* @format: Format by specifier.
* Return: count of chars.
*/
int _printf(const char *format, ...)
{
unsigned int i = 0, count = 0;
va_list args;
int (*f)(va_list);
if (format == NULL)
return (-1);
va_start(args, format);
while (format[i])
{
/*here:*/
for (; format[i] != '%' && format[i]; i++)
{
_putchar(format[i]);
count++;
}
if (!format[i])
return (count);
f = check_for_specifiers(&format[i + 1]);
/**if (format[i] == '%' && format[i + 1] == '%' && format[i + 2] == '%' && format[i + 3] != '%')
* {
* _putchar(format[i+1]);
* _putchar(format[i+2]);
* i = i + 3;
* count++;
* goto here;
*} main fail is mod failure fix it
*/
if (f != NULL)
{
count += f(args);
i += 2;
continue;
}
if (!format[i + 1])
return (-1);
_putchar(format[i]);
count++;
if (format[i + 1] == '%')
i += 2;
else
i++;
}
va_end(args);
return (count);
}