-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
49 lines (48 loc) · 1.15 KB
/
Copy path_printf.c
File metadata and controls
49 lines (48 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
#include "holberton.h"
/**
*_printf - prints an specified format
*@format: format to print
*Return: length of the print
*/
int _printf(const char *format, ...)
{
function_t identifier_f[] = {{'c', _printf_c}, {'s', _printf_s},
{'i', print_number}, {'d', print_number}, {'b', _print_b},
{'o', _print_o}, {'u', _print_u}, {'x', _print_x},
{'X', _print_X}, {'\0', NULL}};
va_list flist;
unsigned int len_printf = 0, i = 0, k = 0, flag = 0;
char j = '\0';
if (format == NULL || (format[i] == '%' && format[1] == '\0'))
return (-1);
va_start(flist, format);
while (format[i])
{
for (; format[i] != '%' && format[i] != '\0'; i++)
{
j = format[i];
len_printf += _putchar(j);
}
flag = i + 1;
if (format[flag] == '%' && format[i])
_putchar('%'), len_printf++, i += 2;
else if (format[flag] == '\0')
i++;
else
{
for (k = 0; identifier_f[k].id && format[i]; k++)
{
if (identifier_f[k].id == format[flag])
{
len_printf += identifier_f[k].f(flist);
i += 2;
break;
}
}
}
if (identifier_f[k].id == '\0' && format[i])
_putchar(format[i++]), len_printf++;
}
va_end(flist);
return (len_printf);
}