forked from fernandogmo/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.c
More file actions
72 lines (65 loc) · 1.2 KB
/
printf.c
File metadata and controls
72 lines (65 loc) · 1.2 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
#include "holberton.h"
#include "funcs_array.h"
/**
* _printf - prints to stdout according to a format string
* @format: constant string containing zero or more directives
* Return: int number of characters printed (excluding terminating null-byte)
*/
int _printf(const char *format, ...)
{
int i, count = 0;
va_list ap;
va_start(ap, format);
if (format == NULL)
return (-1);
for (i = 0; format[i] != '\0'; i++)
{
if (format[i] != '%')
{
count += _putchar(format[i]);
continue;
}
switch (format[++i])
{
case '%':
count += _putchar('%');
break;
case 'c':
case 's':
case 'd':
case 'i':
case 'u':
case 'o':
count += call_print_fn(format[i], ap);
break;
default:
if (!format[i])
return (-1);
count += _putchar('%');
count += _putchar(format[i]);
break;
}
}
va_end(ap);
return (count);
}
/**
* call_print_fn - call appropriate print fn
* @ch: format string character
* @ap: object to be printed
* Return: number of characters printed
*/
int call_print_fn(char ch, va_list ap)
{
int j;
int count = 0;
for (j = 0; funcs[j].spec != NULL; j++)
{
if (ch == funcs[j].spec[0])
{
count += funcs[j].fn(ap);
break;
}
}
return (count);
}