-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
63 lines (58 loc) · 1.01 KB
/
_printf.c
File metadata and controls
63 lines (58 loc) · 1.01 KB
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
#include "main.h"
/**
* handle_specifier - checks and executes matching format specifier
* @c: specifier character
* @ap: argument list
* Return: number of characters printed
*/
int handle_specifier(char c, va_list ap)
{
int k = 0;
spec_t table[] = {
{'c', print_char}, {'s', print_string},
{'%', print_percent}, {'d', print_int},
{'i', print_int}, {'\0', NULL}
};
while (table[k].sp)
{
if (table[k].sp == c)
return (table[k].func(ap));
k++;
}
_putchar('%');
_putchar(c);
return (2);
}
/**
* _printf - produces output according to a format
* @format: format string
* Return: number of characters printed
*/
int _printf(const char *format, ...)
{
va_list ap;
int i = 0;
int count = 0;
if (!format)
return (-1);
va_start(ap, format);
while (format[i])
{
if (format[i] != '%')
{
count += _putchar(format[i]);
i++;
continue;
}
i++;
if (!format[i])
{
va_end(ap);
return (-1);
}
count += handle_specifier(format[i], ap);
i++;
}
va_end(ap);
return (count);
}