-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
78 lines (71 loc) · 1.12 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
#include "holberton.h"
/**
* _printf - printf function.
* @format: variable
*
* Return: nbytes printed.
*/
int _printf(const char *format, ...)
{
va_list list;
unsigned int i = 0, characters_number = 0;
if (!format)
return (-1);
va_start(list, format);
for (i = 0; format[i] != '\0'; i++)
{
if (format[i] == '%')
{
if (format[i + 1] == '\0')
return (-1);
else if (format[i + 1] == '%')
{
_putchar('%');
characters_number++;
i++;
}
else if (cmp_func(format[i + 1]) != NULL)
{
characters_number += (cmp_func(format[i + 1]))(list);
i++;
}
else
{
_putchar(format[i]);
characters_number++;
}
}
else
{
_putchar(format[i]);
characters_number++;
}
}
va_end(list);
return (characters_number);
}
/**
* cmp_func - Entry point
* @a: character.
*
* Return: 0.
*/
int (*cmp_func(const char a))(va_list)
{
print_f printf[] = {
{'c', printc},
{'s', print_string},
{'d', print_n},
{'i', print_n},
{'\0', NULL}
};
int k;
for (k = 0; printf[k].p != '\0'; k++)
{
if (printf[k].p == a)
{
return (printf[k].func);
}
}
return (0);
}