-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
72 lines (63 loc) · 1.25 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
72
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include "main.h"
/**
* run_printf - print formatted strings to console.
* @format: formatted string.
* @args: arguments of type va_list.
* Return: number of chars printed exculding the null byte.
*/
int run_printf(const char *format, va_list args)
{
int len = -1; /*if format is NULL return -1 as len*/
if (format != NULL)
{
int index = 0;
int (*op)(va_list);
len = 0;
while (format[index] != '\0')
{
if (format[index] == '%')
{
if (format[index + 1] == ' ' || format[index + 1] == '%')
{
len += write(1, &format[index], 1);
index++;
}
else
{
op = get_converter(format[index + 1]);
if (op)
len += op(args);
else
{
write(1, &format[index], 1);
write(1, &format[index + 1], 1);
len += 2;
}
index += 1;
}
}
else
len += write(1, &format[index], 1);
index++;
}
}
return (len);
}
/**
* _printf - printd formatted strings to console.
*
* @format: formatted string.
* Return: number of chars printed exculding the null byte.
*/
int _printf(const char *format, ...)
{
int num;
va_list args;
va_start(args, format);
num = run_printf(format, args);
va_end(args);
return (num);
}