-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
49 lines (48 loc) · 1014 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
#include "main.h"
/**
* _printf - Prints formatted output to the standard output
* @format: The format string containing format specifiers.
* Return: The number of characters printed or -1 if the format string is NULL
*/
int _printf(const char *format, ...)
{
va_list argumentList;
int i = 0, characterCount = 0;
int (*formatFunctionPointer)(va_list) = NULL;
va_start(argumentList, format);
if (format == NULL)
return (-1);
while (format[i])
{
if (format[i] != '%')
{
_putchar(format[i]);
i++;
characterCount++;
continue;
}
i++;
formatFunctionPointer = find_format_function(format[i]);
if (formatFunctionPointer != NULL)
{
characterCount += formatFunctionPointer(argumentList);
}
else
{
if (format[i] == '\0')
return (-1);
if (format[i] == '%')
{
characterCount += _putchar('%');
}
else
{
characterCount += _putchar('%');
characterCount += _putchar(format[i]);
}
}
i++;
}
va_end(argumentList);
return (characterCount);
}