-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
134 lines (117 loc) · 2.09 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
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
128
129
130
131
132
133
134
#include "main.h"
/**
* _printf - printf clone
* @format: format specifier
*
* Return: number of characters printed
*/
int _printf(const char *format, ...)
{
va_list args;
int i;
int count = 0;
if (format == NULL)
return (-1);
va_start(args, format);
for (i = 0; format[i] != '\0'; i++)
{
if (format[i] == '%')
{
format++;
count += print_argument(format[i], args);
}
else
{
write(1, &format[i], 1);
count++;
}
}
va_end(args);
return (count);
}
/**
* print_argument - prints the argument according to the format specifier
* @specifier: format specifier
* @args: arguments list
*
* Return: number of characters printed
*/
int print_argument(char specifier, va_list args)
{
switch (specifier)
{
case 'c':
return (print_char(args));
case 's':
return (print_string(args));
case 'S':
return (printf_String(args));
case 'd':
case 'i':
return (print_int(args));
case '%':
write(1, "%", 1);
return (-1);
case 'b':
return (print_binary(args));
case 'p':
return (print_pointer(args));
case 'u':
return (print_unsigned_int(args));
case 'o':
return (print_octal(args));
case 'x':
return (print_hex(args, 0));
case 'X':
return (print_hex(args, 1));
case 'r':
return (print_reverse(args));
case 'R':
return (printrot13string(args));
default:
return (0);
}
}
/**
* print_char - prints a character
* @args: arguments list
*
* Return: number of characters printed
*/
int print_char(va_list args)
{
char c = va_arg(args, int);
write(1, &c, 1);
return (1);
}
/**
* print_int - prints an integer
* @args: arguments list
*
* Return: number of characters printed
*/
int print_int(va_list args)
{
int num = va_arg(args, int);
char str[20];
sprintf(str, "%d", num);
write(1, str, strlen(str));
return (strlen(str));
}
/**
* print_string - prints a string
* @args: arguments list
*
* Return: number of characters printed
*/
int print_string(va_list args)
{
char *s = va_arg(args, char *);
if (s == NULL)
{
write(1, "(null)", 6);
return (6);
}
write(1, s, strlen(s));
return (strlen(s));
}