-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
49 lines (41 loc) · 1 KB
/
_printf.c
File metadata and controls
49 lines (41 loc) · 1 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
#include "_printf.h"
#include "_printf_internals.h"
/**
* _printf - prints a formated string to stdout.
* @format: - pointer to a string with optional format specifiers.
*
* Return: number of characters printed, or -1 on error.
*/
int _printf(const char *format, ...)
{
va_list args;
int bytes_printed = 0, ret_val = 0;
string fmt;
char arr[PRINTF_BUFFER_LENGTH], c = 0;
char_arr buffer;
if (format == NULL)
return (-1);
va_start(args, format);
fmt.size = _strlen(format) + 1, fmt.i = 0, fmt.s = format;
buffer.size = sizeof(arr), buffer.i = 0, buffer.buf = arr;
for (c = string_readc(&fmt); c > 0; c = string_readc(&fmt))
{
if (c != '%')
{
ret_val = buffer_putchar(&buffer, c);
if (ret_val < 0)
return (-1);
bytes_printed += ret_val;
continue;
}
ret_val = format_handler(args, &fmt, &buffer);
if (ret_val < 0)
return (-1);
bytes_printed += ret_val;
}
va_end(args);
ret_val = buffer_flush(&buffer);
if (ret_val < 0)
return (-1);
return (bytes_printed + ret_val);
}