forked from AlfredPianist/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_printf.c
executable file
·48 lines (44 loc) · 1.12 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
#include "holberton.h"
/**
* _printf - Our implementation of the "printf" function.
* @format: The argument list.
*
* Description: Iterates through every character of format and if there's a
* '%' character, it will format its output depending on its
* specifier.
* Return: The length of the printed string without the NUL character
* or -1 if format is NULL.
*/
int _printf(const char *format, ...)
{
va_list arg_l;
int buffer_i;
unsigned int buff_len;
char *buffer;
buffer_i = buff_len = 0, buffer = NULL;
buffer = _alloc(buffer, 1024);
if (!format || !buffer || (*format == '%' && *(format + 1) == '\0'))
{
free(buffer);
return (-1);
}
va_start(arg_l, format);
while (*format != '\0')
{
if (*format == '%')
{
if (*(format + 1) == '\0')
{
va_end(arg_l), write(1, buffer, buffer_i), free(buffer);
return (-1);
}
format++;
form(&format, arg_l, &buffer_i, &buff_len, buffer);
}
else
check_buffer(buffer, &buffer_i, &buff_len, *(format++));
}
va_end(arg_l);
buffer[buffer_i] = '\0', write(1, buffer, buffer_i), free(buffer);
return (buff_len);
}