-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathour_printf.c
More file actions
66 lines (56 loc) · 1.19 KB
/
our_printf.c
File metadata and controls
66 lines (56 loc) · 1.19 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
64
65
66
#include "main.h"
void pr_buffer(char buffer[], int *b_index);
/**
* _printf - prints formatted output
* @format: pointer
*
* Return: number of characters
*/
int _printf(const char *format, ...)
{
int i, print = 0, pr_char = 0;
int flag, width, precision, size, b_index = 0, index = 0;
va_list arg;
char buffer[BUFF_SIZE];
if (format == NULL)
return (-1);
va_start(arg, format);
for (i = 0; format && format[i] != '\0'; i++)
{
if (format[i] != '%')
{
buffer[b_index++] = format[i];
if (b_index == BUFF_SIZE)
pr_buffer(buffer, &b_index);
pr_char++;
}
else
{
pr_buffer(buffer, &b_index);
flag = pr_flag(format, &index);
width = pr_width(format, &index, arg);
precision = pr_precision(format, arg, &index);
size = pr_size(format, &index);
++i;
print = pull_print(format, &index, arg, buffer,
flag, width, size, precision);
if (print == -1)
return (-1);
pr_char += print;
}
}
pr_buffer(buffer, &b_index);
va_end(arg);
return (pr_char);
}
/**
* pr_buffer - print buffer
* @buffer: array
* @b_index: index
*/
void pr_buffer(char buffer[], int *b_index)
{
*b_index = 0;
if (*b_index > 0)
write(1, &buffer[0], *b_index);
}