-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
45 lines (43 loc) · 833 Bytes
/
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
#include "main.h"
#include <stdlib.h>
/**
* _printf - prints any string with certain flags for modification
* @format: the string of characters to write to buffer
* Return: an integer that counts how many writes to the buffer were made
*/
int _printf(const char *format, ...)
{
int i = 0, var = 0;
va_list v_ls;
buffer *buf;
buf = buf_new();
if (buf == NULL)
return (-1);
if (format == NULL)
return (-1);
va_start(v_ls, format);
while (format[i])
{
buf_wr(buf);
if (format[i] == '%')
{
var = opid(buf, v_ls, format, i);
if (var < 0)
{
i = var;
break;
}
i += var;
continue;
}
buf->str[buf->index] = format[i];
buf_inc(buf);
i++;
}
buf_write(buf);
if (var >= 0)
i = buf->overflow;
buf_end(buf);
va_end(v_ls);
return (i);
}