-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
63 lines (55 loc) · 1.31 KB
/
_printf.c
File metadata and controls
63 lines (55 loc) · 1.31 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
52
53
54
55
56
57
58
59
60
61
62
63
#include "main.h"
/**
* print_buffer - function that prints the contents of the buffer if it exist
* @indbuf: the index for buffer
* @buffer: an array of chars
*/
void print_buffer(char buffer[], int *indbuf)
{
if (*indbuf > 0)
write(1, &buffer[0], *indbuf);
*indbuf = 0;
}
/**
* _printf - the Printf function
* @format: a character string composed of zero or more directives
* Return: the printed characters
*/
int _printf(const char *format, ...)
{
int flags, width, precision, size, indbuf = 0;
int j, printed = 0, printed_chars = 0;
char buffer[BUFF_SIZE];
va_list lst;
if (format == NULL)
return (-1);
va_start(lst, format);
for (j = 0; format && format[j] != '\0'; j++)
{
if (format[j] != '%')
{
buffer[indbuf++] = format[j];
if (indbuf == BUFF_SIZE)
print_buffer(buffer, &indbuf);
/* write(1, &format[j], 1); */
printed_chars++;
}
else
{
print_buffer(buffer, &indbuf);
flags = print_flags(format, &j);
width = print_width(format, &j, lst);
precision = print_precision(format, &j, lst);
size = print_size(format, &j);
++j;
printed = handl_print(format, &j, lst, buffer,
flags, width, precision, size);
if (printed == -1)
return (-1);
printed_chars += printed;
}
}
print_buffer(buffer, &indbuf);
va_end(lst);
return (printed_chars);
}