-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprint3.c
71 lines (66 loc) · 1.37 KB
/
print3.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
68
69
70
71
#include "holberton.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
/**
* error_format - returns error
* @format: format
* none
*/
void error_format(const char *format)
{
if (!format || !*format)
{
write(1, "error", 6);
exit(98);
}
}
/**
* _printf - trying to make printf2
* @format: format of string
* Return: number of chars printed
*/
int _printf(const char *format, ...)
{
int i, b_i, l_conv, flag;
char *buffer, *conv, *format_str;
va_list alist;
error_format(format);
buffer = malloc(BUF_LENGTH * sizeof(char));
_flush(buffer);
va_start(alist, format), flag = b_i = 0;
for (i = 0; format[i] != '\0';)
{
if (format[i] != '%')
{
fill_buffer(buffer, format + i, b_i, 1);
i += 1, b_i += 1;
}
if (format[i] == '%')
{
flag = 1, conv = grab_format(format + i);
if (format[i + 1] == '%' || conv == NULL)
{
flag = (flag == 0) ? 1 : 0;
fill_buffer(buffer, format + i, b_i, 1);
i += 2, b_i += 1;
}
}
if (flag == 1)
{
flag = 0;
conv = grab_format(format + i);
l_conv = _strlen(conv);
format_str = get_mstring_func(conv[l_conv - 1])(conv, alist);
free(conv);
fill_buffer(buffer, format_str, b_i, _strlen(format_str));
b_i = b_i + _strlen(format_str);
free(format_str), i += l_conv;
}
}
print_buffer(buffer, b_i);
free(buffer);
return (b_i);
}