-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
81 lines (78 loc) · 1.53 KB
/
_printf.c
File metadata and controls
81 lines (78 loc) · 1.53 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
#include "holberton.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
/**
* look_forpercent - Look for the correct percent for print
* @format: the recieve format
* Return: The correct percent to the function
*/
int (*look_forpercent(const char *format))(va_list)
{
unsigned int x;
fmt percent[] = {
{"c", print_char},
{"s", print_str},
{"i", print_int},
{"d", print_dec},
{"r", print_revstr},
{"S", print_strnonprint},
{"R", print_rot13},
{"b", print_binary},
{"u", print_sign_to_unsigned},
{"o", print_octal},
{"X", print_hexa_upper},
{"x", print_hexa_lower},
{NULL, NULL}
};
for (x = 0; percent[x].ptr != NULL; x++)
{
if (*(percent[x].ptr) == *format)
{
break;
}
}
return (percent[x].f);
}
/**
* _printf - The principal function printf
* @format: the recieve format
* Return: Count of chars printed
*/
int _printf(const char *format, ...)
{
unsigned int countfr = 0, count = 0;
va_list list;
int (*f)(va_list);
if (format == NULL)
return (-1);
va_start(list, format);
while (format[countfr])
{
for (; format[countfr] != '%' && format[countfr]; countfr++)
{
_putchar(format[countfr]);
count++;
}
if (!format[countfr])
return (count);
f = look_forpercent(&format[countfr + 1]);
if (f != NULL)
{
count += f(list);
countfr += 2;
continue;
}
if (!format[countfr + 1])
return (-1);
_putchar(format[countfr]);
count++;
if (format[countfr + 1] == '%')
countfr += 2;
else
countfr++;
}
va_end(list);
return (count);
}