-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
92 lines (87 loc) · 1.54 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
#include "main.h"
/**
* find_spec - finds spec
* @fmt: specifier
* @num: size of fmt[]
* @specifier: from theformat
* Return: -1
*/
int find_spec(fmt_s *fmt, int num, char specifier)
{
int i;
for (i = 0; i < num; i++)
{
if (specifier == fmt[i].specifier)
return (i);
}
return (-1);
}
/**
* process_fmt - process fmt
* @format: specifier
* @list: arguments list
* @fmt: specifiers and functions
* @num: size of fmt
* Return: count
*/
int process_fmt(const char *format, va_list list, fmt_s *fmt, int num)
{
int count = 0, index;
while (*format)
{
if (*format == '%' && *(format + 1))
{
format++;
if (*format == '%')
count += _putchar('%');
else
{
index = find_spec(fmt, num, *format);
if (index >= 0)
count += fmt[index].fmt(list);
else
{
count += _putchar('%');
count += _putchar(*format);
}
}
}
else if (*format == '%' && *(format + 1) == '\0')
{
return (-1);
}
else
count += _putchar(*format);
format++;
}
return (count);
}
/**
* _printf - performs printf
* @format: format specs
*
* Return: count
*/
int _printf(const char *format, ...)
{
va_list list;
int count = 0, num;
fmt_s fmt[] = {
{'c', handle_char},
{'s', handle_str},
{'d', handle_int},
{'i', handle_int},
{'b', handle_bin},
{'u', handle_u},
{'o', handle_o},
{'x', handle_x},
{'X', handle_X},
};
if (format == NULL)
return (-1);
va_start(list, format);
num = sizeof(fmt) / sizeof(fmt[0]);
count = process_fmt(format, list, fmt, num);
va_end(list);
return (count);
}