forked from nickuchida/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
92 lines (88 loc) · 1.51 KB
/
_printf.c
File metadata and controls
92 lines (88 loc) · 1.51 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
#include "holberton.h"
#include <stdio.h>
#include <stdlib.h>
/**
* _printf - prints data and information
* @format: the data to be printed
* Return: returns the number of characters printed
*/
int _printf(const char *format, ...)
{
va_list ap;
int i = 0, result = 0;
va_start(ap, format);
while (format[i])
{
if (format == NULL)
return (-1);
if (format[i] != '%')
{
print_c(format[i]);
result++;
}
else if (format[i] == '%')
{
switch (format[i + 1])
{
case '\0':
va_end(ap);
return (-1);
case '%':
result += print_p();
i++;
break;
case 'c': case 's': case 'd': case 'i': case 'R':
result += ext1_printf(ap, format[i + 1], &i);
break;
default:
result += print_p();
print_c(format[i + 1]);
result++;
i++;
break;
}
}
i++;
}
return (result);
}
/**
* ext1_printf - function extension for printf
* @ap: va list
* @ch: case character
* @p: pointer to index
* Return: result to the main printf result
*/
int ext1_printf(va_list ap, char ch, int *p)
{
int result = 0;
switch (ch)
{
case 'c':
result += print_c(va_arg(ap, int));
*p = *p + 1;
break;
case 's':
result += print_s(va_arg(ap, char *));
*p = *p + 1;
break;
case 'd':
result += print_d(va_arg(ap, int));
*p = *p + 1;
break;
case 'i':
result += print_i(va_arg(ap, int));
*p = *p + 1;
break;
case 'R':
result += print_rot(va_arg(ap, char *));
*p = *p + 1;
break;
case 'z':
print_p();
result++;
*p = *p + 1;
break;
}
return (result);
}