-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
70 lines (60 loc) · 1.23 KB
/
_printf.c
File metadata and controls
70 lines (60 loc) · 1.23 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
#include "main.h"
/**
* check_format - checks for a valid specifier
* @c: the character to be checked as a specifier
*
* Return: 1 if a valid specifier is found otherwise 0
*/
int check_format(char c)
{
if (c == '%' || c == 'c' || c == 's' || c == 'd' ||
c == 'i' || c == 'b' || c == 'r' || c == 'R')
return (1);
return (0);
}
/**
* _printf - writes output to stdout, the standard output stream
* @format: format is a character string.
* The format string is composed of zero or more directives
*
* Return: the number of characters printed
* (excluding the null byte used to end output to strings)
*/
int _printf(const char *format, ...)
{
int i = 0, count = 0;
va_list args;
int (*spef_func)(va_list);
if (format == NULL)
return (-1);
va_start(args, format);
while (format && format[i])
{
if (format[i] == '%' && (check_format(format[i + 1]) ||
format[i + 1] == '\0'))
{
i++;
switch (format[i])
{
case '%':
_putchar('%');
count++;
break;
default:
spef_func = get_spefs_func(&format[i]);
if (spef_func == NULL)
return (-1);
count += spef_func(args);
break;
}
}
else
{
_putchar(format[i]);
count++;
}
i++;
}
va_end(args);
return (count);
}