-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
71 lines (60 loc) · 1.32 KB
/
_printf.c
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
#include "main.h"
/**
* execute_func - finds the format func
* @s: the format string
* @arg: argument pointer
*
* Return: the number of bytes printed
*/
int execute_func(char *s, va_list arg)
{
int (*print_func)(va_list) = find_format_handlers(s);
if (print_func)
return (print_func(arg));
return (0);
}
/**
* produce_range - outputs characters within a specified address range
* @start: start address
* @stop: stop address
*
* Return: number bytes emitted
*/
int produce_range(char *start, char *stop)
{
int r_value = 0;
for (; start <= stop; start++)
r_value += _putchar(*start);
return (r_value);
}
/**
* _printf - function that generatess output in accordance to a given format
* @format: format (char, string, int, decimal)
* Return: size the output text;
*/
int _printf(const char *format, ...)
{
unsigned int r_value = 0;
char *ptr = (char *)format, *specifier_start;
va_list args;
va_start(args, format);
if (format == NULL || (format[0] == '%' && !format[1]))
return (-1);
for (; *ptr; ptr++)
{
if (*ptr != '%')
{
r_value += _putchar(*ptr);
continue;
}
specifier_start = ptr;
ptr++;
if (find_format_handlers(ptr))
r_value += execute_func(ptr, args);
else
r_value += produce_range(specifier_start, ptr);
}
_putchar(BUF_CLEARING);
va_end(args);
return (r_value);
}