-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
50 lines (49 loc) · 831 Bytes
/
_printf.c
File metadata and controls
50 lines (49 loc) · 831 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
#include "holberton.h"
/**
* _printf - Printf function copy
* Description: This is a modified version of printf
* @format: String to print
* Return: Number of characters printed
*/
int _printf(const char *format, ...)
{
int how_many = 0;
const char *pf;
int (*f)();
char *buffer = buffer_init();
va_list args;
va_start(args, format);
if (!buffer)
return (0);
if (!format || (format[0] == '%' && format[1] == '\0'))
{
free(buffer);
return (-1);
}
for (pf = format; *pf; pf++)
{
if (*pf == '%')
{
f = verify_format(pf);
if (f)
{
how_many += f(args, buffer);
pf++;
}
else
{
_putchar(buffer, *pf);
how_many++;
}
}
else
{
_putchar(buffer, *pf);
how_many++;
}
}
va_end(args);
buffer_print(buffer, buffer_pos(buffer));
free(buffer);
return (how_many);
}