-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
51 lines (50 loc) · 1000 Bytes
/
_printf.c
File metadata and controls
51 lines (50 loc) · 1000 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
#include "main.h"
/**
* _printf - Prints any type of data based
* on format.
*
* @format: Format to print (int, double, char, string, ...)
* Return: int
*/
int _printf(const char *format, ...)
{
va_list data_input;
unsigned int i = 0;
char *print_output, *buffer;
void *(*r)(va_list, char **buffer);
print_output = _calloc(3000, sizeof(char));
if (print_output == NULL)
return (-1);
buffer = &print_output[0];
va_start(data_input, format);
if (format == NULL)
return (-1);
for (; format && format[i]; i++)
{
*buffer = format[i];
buffer++;
if (format[i] == '%')
{
if (format[i + 1] == '\0')
{
free(print_output);
return (-1);
} r = match(format + i + 1);
if (r == NULL)
{
if (format[i + 1] == '%')
i++;
continue;
} else
{
buffer--;
r(data_input, &buffer);
i++;
}
}
} i = _strlen(print_output);
_putchar(print_output, buffer - (char *)print_output);
va_end(data_input);
free(print_output);
return (i);
}