-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
127 lines (122 loc) · 2.5 KB
/
Copy path_printf.c
File metadata and controls
127 lines (122 loc) · 2.5 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "main.h"
/**
* hswitch - printf implementation
* @s: the char to be checked as a specefier.
* @args: the argument to match the speci
* @len: the lgnth to be a return value
* @x: integer.
* @format: format.
* Return: the wanted result as printf would print
*/
static int hswitch(char s, va_list args, int *len, int *x, const char *format)
{
if (!s)
return (2);
switch (s)
{
case 'S':
Non_printable(va_arg(args, char *), len);
break;
case 'p':
print_ptr((unsigned long)va_arg(args, void *), len);
break;
case 'R':
rot13(va_arg(args, char *), len);
break;
case 'r':
print_rev(va_arg(args, char *), len);
break;
case '-':
if (mflag(args, len, x, format) == -1)
return (2);
break;
default:
return (1);
}
return (0);
}
/**
* _switch - printf implementation
* @s: the char to be checked as a specefier.
* @args: the argument to match the speci
* @len: the lgnth to be a return value
* @x: integer.
* @format: format.
* Return: the wanted result as printf would print
*/
static int _switch(char s, va_list args, int *len, int *x, const char *format)
{
char a;
switch (s)
{
case 'c':
a = va_arg(args, int);
*len += write(1, &a, 1);
break;
case 's':
*len += printstr(va_arg(args, char *));
break;
case '%':
*len += write(1, "%", 1);
break;
case 'd': case 'i':
print_num(va_arg(args, int), 10, 1, 0, len);
break;
case 'b':
print_num(va_arg(args, int), 2, 0, 0, len);
break;
case 'u':
print_num(va_arg(args, int), 10, 0, 0, len);
break;
case 'o':
print_num(va_arg(args, int), 8, 0, 0, len);
break;
case 'x':
print_num(va_arg(args, int), 16, 0, 0, len);
break;
case 'X':
print_num(va_arg(args, int), 16, 0, 1, len);
break;
default:
a = hswitch(s, args, len, x, format);
if (a == 1)
return (1);
if (a == 2)
return (2);
}
return (0);
}
/**
* _printf - printf implementation function that
* produces output according to a format just like the built-in function.
* @format: the input
* Return: the wanted result as printf would print
*/
int _printf(const char *format, ...)
{
int x, len, m;
va_list args;
va_start(args, format);
x = 0;
len = 0;
if (format == NULL)
return (-1);
while (format[x])
{
if (format[x] == '%')
{
m = _switch(format[x + 1], args, &len, &x, format);
if (m == 2)
return (-1);
else if (m == 1)
len += write(1, format + x, 1);
else
x++;
}
else
len += write(1, format + x, 1);
x++;
}
va_end(args);
return (len);
}