-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
63 lines (54 loc) · 963 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "main.h"
#include <unistd.h>
#include <stdio.h>
/**
* _printf - function that takes variable number of arguments
* and prints them or point to another function to be called to print
* different formats
*
* @format: the string to be printed
*
* Return: the number of printed characters (int)
*/
int _printf(const char *format, ...)
{
va_list ap;
int i, chars, temp;
va_start(ap, format);
chars = 0;
temp = 0;
if (!format)
return (-1);
for (i = 0; format[i]; i++)
{
if (format[i] == '%')
{
if (!format[i + 1] || (format[i + 1] == ' ' && !format[i + 2]))
return (-1);
temp = fn_control(&format[i + 1], ap);
chars += temp;
if (temp == 0 && format[i + 1] == '%')
{
_putchar(format[i + 1]);
chars++;
i++;
}
else if (temp == 0)
{
_putchar(format[i]);
chars++;
}
else
{
i++;
}
}
else
{
_putchar(format[i]);
chars++;
}
}
va_end(ap);
return (chars);
}