-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
110 lines (98 loc) · 2.01 KB
/
_printf.c
File metadata and controls
110 lines (98 loc) · 2.01 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "main.h"
/**
* check_a_print - check the code.
* @format : variable
* @frankenstein : variable
* Return: check declaration
*/
int (*check_a_print(const char *format, int *frankenstein))(va_list arg_ptr)
{
print_option ops[] = {
{"%c", print_char},
{"%s", print_string},
{"%r", print_rev_string},
{"%S", print_n_string},
{"%R", print_r_string},
{"%i", print_int},
{"%+i", print_int_plus},
{"% i", print_int_space},
{"% +i", print_int_plus},
{"%+ i", print_int_plus},
{"%d", print_int},
{"%+d", print_int_plus},
{"% d", print_int_space},
{"% +d", print_int_plus},
{"%+ d", print_int_plus},
{"%b", print_bin},
{"%u", print_unsigned},
{"%o", print_octal},
{"%#o", print_octal_tag},
{"%x", print_hex},
{"%#x", print_hex_tag},
{"%p", print_addr},
{"%X", print_hex_u},
{"%#X", print_hex_u_tag},
{"%%", print_percent},
{"% % ", print_percent_2_space},
{"% ", print_percent_space},
{"ok", print_buffer_return}
};
int i, j;
i = 0;
if (format[0] == '%' && format[1] == '\0')
return (print_none_return);
while (i < 27)
{
j = 0;
while (format[j] == (ops + i)->specifier[j] && format[j] != '\0')
{
j++;
if ((ops + i)->specifier[j] == '\0')
{
*frankenstein = j - 1;
return ((ops + i)->function);
}
}
i++;
}
return ((ops + i)->function);
}
/**
* _printf - check code.
* @format : variable
* Return: check declaration
*/
int _printf(const char *format, ...)
{
va_list arg_ptr;
int i;
long int cnt, pls;
int *frankenstein;
va_start(arg_ptr, format);
if (format == NULL)
return (-1);
i = 0;
cnt = pls = 0;
if (format[i] == '\0')
return (0);
frankenstein = malloc(sizeof(int));
if (frankenstein == NULL)
return (-1);
while (format[i] != '\0')
{
pls = check_a_print(format + i, frankenstein)(arg_ptr);
if (pls == -1)
pls = print_buffer(format + i);
else if (pls == -2)
pls = 0;
else
i = i + *frankenstein;
cnt += pls;
i++;
}
va_end(arg_ptr);
free(frankenstein);
if (cnt == 0)
return (-1);
return (cnt);
}