-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
71 lines (68 loc) · 1.41 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "main.h"
/**
* _printf - The printf function.
* @format: A constant char.
*
* Return: The number of characters printed.
*/
int _printf(const char *format, ...)
{
int charcount = 0;
va_list ptr;
va_start(ptr, format);
if (format == NULL)
return (-1);
if (!format || (format[0] == '%' && !format[1]))
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (-1);
charcount = _printf_helper(format, ptr, charcount);
va_end(ptr);
return (charcount);
}
/**
* _printf_helper - helper function.
* @format: A constant char.
* @ptr: pointer.
* @charcount: number of chars.
* Return: The number of characters printed.
*/
int _printf_helper(const char *format, va_list ptr, int charcount)
{
while (*format)
{
if ((*format == '%') && (*(format + 1) == 'c'))
{
charcount += print_char(ptr);
format += 2;
}
else if ((*format == '%') && (*(format + 1) == 's'))
{
charcount += print_string(ptr);
format += 2;
}
else if ((*format == '%') && (*(format + 1) == '%'))
{
charcount += print_pourcentage();
format += 2;
}
else if ((*format == '%') && (*(format + 1) == 'b'))
{
charcount += print_binary(ptr);
format += 2;
}
else if ((*format == '%') &&
((*(format + 1) == 'd') || (*(format + 1) == 'i')))
{
charcount += print_int(ptr);
format += 2;
}
else
{
write(1, format, 1);
charcount++;
format++;
}
}
return (charcount);
}