-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
67 lines (64 loc) · 1.07 KB
/
_printf.c
File metadata and controls
67 lines (64 loc) · 1.07 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
#include "holberton.h"
/**
* _printf - Entry point
* Desc: Entry
*@format: pointer
* Return: On success.
*/
int _printf(const char *format, ...)
{
va_list mylist;
unsigned int i = 0, j = 0;
if (!format || (format[0] == '%' && format[1] == '\0'))
return (-1);
va_start(mylist, format);
for (i = 0; format[i] != '\0'; i++)
{
if (format[i] == '%')
{
if (format[i + 1] == '%')
{ _putchar('%');
j++;
i++;
}
else if (get_op_func(format, i + 1) != NULL)
{ j += (get_op_func(format, i + 1))(mylist);
i++;
}
else
{ _putchar(format[i]);
j++;
}
}
else
{ _putchar(format[i]);
j++;
}
}
va_end(mylist);
return (j);
}
/**
* get_op_func - Entry function
* @s: operator
* @pos: position
* Return: function
*/
int (*get_op_func(const char *s, int pos))(va_list)
{
print_fun ops[] = {
{"c", print_single_char},
{"s", print_string_char},
{"d", print_decimal},
{"i", print_decimal},
{NULL, NULL}};
int k;
for (k = 0; ops[k].op != NULL; k++)
{
if (ops[k].op[0] == s[pos])
{
return (ops[k].f);
}
}
return (NULL);
}