-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
50 lines (45 loc) · 900 Bytes
/
_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
#include "main.h"
#include <unistd.h>
#include <stdarg.h>
/**
* _printf - Emulate the original printf function.
* @format: Format string.
* Return: Number of characters printed.
*/
int _printf(const char *format, ...)
{
int i = 0, count = 0;
va_list args;
int (*format_func)(va_list);
va_start(args, format);
if (!format || (format[0] == '%' && !format[1]))
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (-1);
while (format[i])
{
if (format[i] == '%')
{
if (!format[i + 1] || (format[i + 1] == ' ' && !format[i + 2]))
return (-1);
format_func = get_function(format + i + 1);
if (format_func != NULL)
{
count += format_func(args);
}
else
{
count += _putchar(format[i]);
count += _putchar(format[i + 1]);
}
i++;
}
else
{
count += _putchar(format[i]);
}
i++;
}
va_end(args);
return (count);
}