-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
executable file
·76 lines (73 loc) · 1.15 KB
/
_printf.c
File metadata and controls
executable file
·76 lines (73 loc) · 1.15 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
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "holberton.h"
/**
*get_f - output format
*@s: first argument
*Return: numbers of characters
*/
int (*get_f(char s))(va_list)
{
arg_t options[] = {
{'c', printCha},
{'s', printStr},
{'d', printNum},
{'i', printNum},
{'b', printBin},
{'R', printR13},
{'r', printRev},
{'\0', NULL}
};
unsigned int j = 0;
for (j = 0 ; options[j].letter ; j++)
{
if (s == options[j].letter)
return (options[j].f);
}
return (NULL);
}
/**
* _printf - custom printf
* @format: specifier
* Return: bytes printed
*/
int _printf(const char *format, ...)
{
va_list a_list;
int i = 0, nbyte = 0;
int (*fun)(va_list);
va_start(a_list, format);
if (format == NULL || (format[0] == '%' && format[1] == '\0'))
return (-1);
for (i = 0 ; format[i] ; i++)
{
if (format[i] == '%')
{
if (format[i + 1] == '%')
{
write(1, &format[i], 1);
nbyte++;
i++;
}
else
{
fun = get_f(format[i + 1]);
if (fun)
{
nbyte += fun(a_list);
i++;
}
else
{
write(1, &format[i], 1);
nbyte++;
}
}
}
else
{
write(1, &format[i], 1);
nbyte++;
}
}
va_end(a_list);
return (nbyte);
}