-
Notifications
You must be signed in to change notification settings - Fork 0
/
printf.c
87 lines (82 loc) · 1.86 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
#include "main.h"
/**
* _printf - produces output according to a format
* @format: format string that contains plain text and conversion
* specifications
*
* Return: number of characters written
*/
int _printf(const char *format, ...)
{
va_list args;
int count = 0;
int *count_ptr = &count;
va_start(args, format);
if (format == NULL || (format[0] == 37 && format[1] == 0))
return (-1);
printcs(format, count_ptr, args);
va_end(args);
return (count);
}
/**
* printcs - print format string, arguments and update return value
* @format: format string
* @count_ptr: pointer to count
* @args: args
*
* Return: void.
*/
void printcs(const char *format, int *count_ptr, va_list args)
{
while (*format)
{
if (*format == '%')
{
format++;
switch (*format)
{
case 'c':
*count_ptr += print_char(args);
break;
case 's':
*count_ptr += print_str(args);
break;
case 'd':
case 'i':
*count_ptr += print_int(va_arg(args, int));
break;
case 'b':
*count_ptr += print_binary(va_arg(args, unsigned int));
break;
case 'u':
*count_ptr += print_unsigned(va_arg(args, unsigned int), 10);
break;
case 'o':
*count_ptr += print_unsigned(va_arg(args, unsigned int), 8);
break;
case 'x':
*count_ptr += print_unsigned(va_arg(args, unsigned int), 16);
break;
case 'X':
*count_ptr += print_unsigned_uppercase(va_arg(args, unsigned int), 16);
break;
case 'S':
*count_ptr += print_custom_S(va_arg(args, char *));
break;
case 'p':
*count_ptr += print_pointer_hash(va_arg(args, void *));
break;
case '%':
*count_ptr += print_percent();
break;
default:
_putchar('%');
*count_ptr += 1 + _putchar(*format);
break;
}
}
else
*count_ptr += _putchar(*format);
format++;
}
}