-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
52 lines (49 loc) · 1022 Bytes
/
_printf.c
File metadata and controls
52 lines (49 loc) · 1022 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
46
47
48
49
50
51
52
#include "holberton.h"
/**
* _printf - produces output according to a format.
* @format: format to be printed
*
* Return: the number of characters printed
* (excluding the null byte used to end output to strings)
*/
int _printf(const char *format, ...)
{
va_list valist;
op_t operate;
options formatto;
int i = 0, bytes = 0;
va_start(valist, format);
for (; format != NULL && format[i] != '\0'; i++)
{
if (format[i] == '%')
{
if (format[i + 1] != '\0')
formatto = getformat(format, &i);
else
return (-1);
}
else
{
if (((format[i] >= 32 && format[i] <= 126)
|| (format[i] >= 7 && format[i] <= 13)))
{
write(1, &format[i], 1);
bytes++;
}
continue;
}
operate.f = getfunction(formatto.type);
if (operate.f != NULL)
bytes += operate.f(valist, formatto);
else
{
bytes += write(1, &format[i - 1], 1);
if (format[i] != '%')
bytes += write(1, &format[i], 1);
}
}
if (format == NULL)
return (-1);
va_end(valist);
return (bytes);
}