-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
91 lines (80 loc) · 1.51 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "main.h"
#include <unistd.h>
/**
* _printf - function that produces output according to a format
* @format: format of printf
*
* Return: length of printed chars or -1 on failure
**/
int _printf(const char *format, ...)
{
char *s, *si;
int l = 0, i = 0, j = 0, k = 0, x = 0;
va_list ap;
types ops[] = {{'c', pchr}, {'s', pstr}, {'i', pint}, {'d', pint},
{'%', pper}, {'b', pbi}, {'o', poct}, {'u', punsign},
{'x', phex}, {'X', pheX}, {'r', prev},{'R', prot}, {0, NULL}};
if (format == NULL || (_strlen(format) == 1 && format[i] == '%'))
return (-1);
va_start(ap, format);
s = malloc(0);
if (s == NULL)
return (-1);
while (format[i])
{
if (format[i] == '%')
{
j = 0;
while (ops[j].tp && ops[j].tp != format[i + 1])
j++;
if (ops[j].tp == format[i + 1])
{
si = ops[j].f(ap);
if (si == NULL)
{
free(s);
free(si);
return (-1);
}
s = _realloc(s, k, (k + _strlen(si)));
if (s == NULL)
{
free(s);
free(si);
return (-1);
}
x = 0;
if (ops[j].tp == 'c' && si[0] == '\0')
x = 1;
for (l = 0; l < _strlen(si) + x; l++)
s[k++] = si[l];
free(si);
i += 2;
}
else
{
s = _realloc(s, k, k + 1);
if (s == NULL)
{
free(s);
return (-1);
}
s[k++] = format[i++];
}
}
else
{
s = _realloc(s, k, k + 1);
if (s == NULL)
{
free(s);
return (-1);
}
s[k++] = format[i++];
}
}
va_end(ap);
l = write(STDOUT_FILENO, s, k);
free(s);
return (l);
}