-
Notifications
You must be signed in to change notification settings - Fork 0
/
printf.c
94 lines (88 loc) · 1.52 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "main.h"
/**
* _printf - produces output according to format.
* @format: character string
*
* Return: number of characters.
*/
int _printf(const char *format, ...)
{
va_list args;
int count = 0;
if (format == NULL)
return (-1);
va_start(args, format);
while (*format != '\0')
{
if (*format == '%')
{
format++;
if (*format != '\0')
{
count = handle_format(format, args, count);
}
}
else
{
putChar(*format);
count++;
}
format++;
}
va_end(args);
return (count);
}
/**
* handle_format - handles the format specifiers
* @format: format string
* @args: argument list
* @count: current count of printed characters
*
* Return: updated count of printed characters
*/
int handle_format(const char *format, va_list args, int count)
{
switch (*format)
{
case 'b':
count += print_binary(args);
break;
case 'c':
count += print_character(args);
break;
case '%':
count += print_percent(args);
break;
case 's':
count += print_string(args);
break;
case 'i':
count += print_number(args);
break;
case 'd':
count += print_number(args);
break;
case 'u':
count += print_unsigned(args);
break;
case 'o':
count += print_octal(args);
break;
case 'x':
count += print_hex_lower(args);
break;
case 'X':
count += print_hex_upper(args);
break;
case 'S':
count += print_custom_string(args);
break;
case 'p':
count += print_pointer(args);
break;
default:
_printDefault(format);
count += 2;
}
return (count);
}