-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
51 lines (48 loc) · 1.02 KB
/
_printf.c
File metadata and controls
51 lines (48 loc) · 1.02 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
#include "holberton.h"
/**
*_printf - prints a string, char, integer, see the manual for more info.
*@format: string to be printed with options inside
*Return: amount of characters printed
*/
int _printf(const char *format, ...)
{
unsigned int i = 0, cont = 0, mallocInd = 0, tmp = 0;
int (*f)(va_list, char *) = NULL;
char *s = malloc(5000 * sizeof(char));
va_list(valist);
va_start(valist, format);
if (s == NULL || format == NULL || (_strlen(format) == 1 && *format == '%'))
return (-1);
while (format != NULL && *(format + i) != '\0')
{
f = NULL;
if (*(format + i) == '%')
{
i++;
f = get_opc(format + i);
if (f != NULL)
{
tmp = f(valist, s + mallocInd);
cont += tmp - 2;
mallocInd += tmp - 1;
}
else
{
*(s + mallocInd) = '%';
mallocInd++;
}
}
(f == NULL) ? *(s + mallocInd) = *(format + i) : 0;
i++;
mallocInd++;
if (mallocInd >= 1024)
{
_putchar(s, mallocInd);
mallocInd = 0;
}
}
_putchar(s, mallocInd);
free(s);
va_end(valist);
return (i + cont);
}