-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_printf.c
68 lines (56 loc) · 1.52 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
#include "main.h"
void print_buffer(char buffer[], int *buff_ind);
/**
_printf - A function that prints formatted output to stdout.
@format: A string that specifies the format of the output.
Return: The number of characters printed to stdout.
*/
int _printf(const char *format, ...)
{
int a, printed = 0, printed_chars = 0;
int tag, width, precision, size, buff_ind = 0;
va_list list;
char buffer[BUFF_SIZE];
if (format == NULL)
return (-1);
va_start(list, format);
for (a = 0; format && format[a] != '\0'; a++)
{
if (format[a] != '%')
{
buffer[buff_ind++] = format[a];
if (buff_ind == BUFF_SIZE)
print_buffer(buffer, &buff_ind);
/* write(1, &format[a], 1);*/
printed_chars++;
}
else
{
print_buffer(buffer, &buff_ind);
tag = get_flags(format, &a);
width = get_width(format, &a, list);
precision = get_precision(format, &a, list);
size = get_size(format, &a);
++a;
printed = handle_print(format, &a, list, buffer,
tag, width, precision, size);
if (printed == -1)
return (-1);
printed_chars += printed;
}
}
print_buffer(buffer, &buff_ind);
va_end(list);
return (printed_chars);
}
/**
* print_buffer -This function will print the contents of the buffer if it exists.
* @buffer: This is an array consisting of characters.
* @buff_ind: This parameter is an index indicating the position at which the next character should be added to the array.
*/
void print_buffer(char buffer[], int *buff_ind)
{
if (*buff_ind > 0)
write(1, &buffer[0], *buff_ind);
*buff_ind = 0;
}