-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
85 lines (76 loc) · 1.52 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
#include "main.h"
/**
* _printf - prints anything
* @format: list of arguments passed to the function
* Return: number of characters printed
*/
int _printf(const char *format, ...)
{
int char_count = 0;
form_spec specifiers[] = {
{"c", print_char},
{"s", print_str},
{"%", print_percent},
{"d", print_int},
{"i", print_int},
{"b", print_bin},
{"r", print_rev},
{"R", print_rot13},
{"S", print_STR},
{"p", print_addr},
{"u", print_unsigned},
{"o", print_oct},
{"x", print_hex},
{"X", print_HEX},
{NULL, NULL}
};
va_list args;
if (!format)
return (-1);
va_start(args, format);
char_count = printer(format, specifiers, args);
va_end(args);
return (char_count);
}
/**
* printer - prints anything
* @format: list of arguments passed to the function
* @specifiers: list of specifiers
* @args: list of arguments
* Return: number of characters printed
*/
int printer(const char *format, form_spec specifiers[], va_list args)
{
int i = 0, j, char_count = 0, checker;
while (format[i])
{
if (format[i] == '%')
{
i++;
if (format[i] == '\0')
return (-1);
for (j = 0; specifiers[j].c != NULL; j++)
{
if (format[i] == specifiers[j].c[0])
{
checker = specifiers[j].f(args);
if (checker == -1)
return (-1);
char_count += checker;
break;
}
}
if (specifiers[j].c == NULL)
{
char_count += print_percent(args);
char_count += _putchar(format[i]);
}
}
else
{
char_count += _putchar(format[i]);
}
i++;
}
return (char_count);
}