-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
51 lines (49 loc) · 1.17 KB
/
_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
51
#include "main.h"
/**
* _printf - formatted output conversion and print data.
* @format: input string.
*
* Return: number of chars printed.
*/
int _printf(const char *format, ...)
{
unsigned int i = 0, len = 0, ibuf = 0;
va_list arguments;
int (*function)(va_list, char *, unsigned int);
char *buffer;
va_start(arguments, format), buffer = malloc(sizeof(char) * 1024);
if (!format || !buffer || (format[i] == '%' && !format[i + 1]))
return (-1);
if (!format[i])
return (0);
for (i = 0; format && format[i]; i++)
{
if (format[i] == '%')
{
if (format[i + 1] == '\0')
{ print_buf(buffer, ibuf), free(buffer), va_end(arguments);
return (-1);
}
else
{ function = get_print_func(format, i + 1);
if (function == NULL)
{
if (format[i + 1] == ' ' && !format[i + 2])
return (-1);
handl_buf(buffer, format[i], ibuf), len++, i--;
}
else
{
len += function(arguments, buffer, ibuf);
i += ev_print_func(format, i + 1);
}
} i++;
}
else
handl_buf(buffer, format[i], ibuf), len++;
for (ibuf = len; ibuf > 1024; ibuf -= 1024)
;
}
print_buf(buffer, ibuf), free(buffer), va_end(arguments);
return (len);
}