-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
45 lines (42 loc) · 989 Bytes
/
_printf.c
File metadata and controls
45 lines (42 loc) · 989 Bytes
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
#include "main.h"
/**
* _printf - is a function that selects the correct function to print.
* @format: identifier to look for.
* Return: the length of the string.
*/
int _printf(const char * const format, ...)
{
convert_match m[] = {
{"%s", printf_string}, {"%c", printf_char},
{"%%", printf_37},
{"%i", printf_int}, {"%d", printf_dec}, {"%r", printf_srev},
{"%R", printf_rot13}, {"%b", printf_bin}, {"%u", printf_unsigned},
{"%o", printf_oct}, {"%x", printf_hex}, {"%X", printf_HEX},
{"%S", printf_exclusive_string}, {"%p", printf_pointer}
};
va_list args;
int i = 0, j, len = 0;
va_start(args, format);
if (format == NULL || (format[0] == '%' && format[1] == '\0'))
return (-1);
Here:
while (format[i] != '\0')
{
j = 13;
while (j >= 0)
{
if (m[j].id[0] == format[i] && m[j].id[1] == format[i + 1])
{
len += m[j].f(args);
i = i + 2;
goto Here;
}
j--;
}
_putchar(format[i]);
len++;
i++;
}
va_end(args);
return (len);
}