-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
104 lines (99 loc) · 2.08 KB
/
print.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "main.h"
/**
* check_buffer_overflow - if writing over buffer space,
* print everything then revert length back to 0 to write at buffer start
* @buffer: buffer holding string to print
* @len: position in buffer
* Return: length position
*/
int check_buffer_overflow(char *buffer, int len)
{
if (len > 1020)
{
write(1, buffer, len);
len = 0;
}
return (len);
}
/**
* _printf - mini printf version
* @format: initial string with all identifiers
* Return: strings with identifiers expanded
*/
int _printf(const char *format, ...)
{
int len = 0, total_len = 0, i = 0, j = 0;
va_list list;
char *buffer, *str;
char* (*f)(va_list);
if (format == NULL)
return (-1);
buffer = create_buffer();
if (buffer == NULL)
return (-1);
va_start(list, format);
while (format[i] != '\0')
{
if (format[i] != '%') /* copy format into buffer until '%' */
{
len = check_buffer_overflow(buffer, len);
buffer[len++] = format[i++];
total_len++;
}
else /* if %, find function */
{
i++;
if (format[i] == '\0') /* handle single ending % */
{
va_end(list);
free(buffer);
return (-1);
}
if (format[i] == '%') /* handle double %'s */
{
len = check_buffer_overflow(buffer, len);
buffer[len++] = format[i];
total_len++;
}
else
{
f = get_func(format[i]); /* grab function */
if (f == NULL) /* handle fake id */
{
len = check_buffer_overflow(buffer, len);
buffer[len++] = '%';
total_len++;
buffer[len++] = format[i];
total_len++;
}
else /* return string, copy to buffer */
{
str = f(list);
if (str == NULL)
{
va_end(list);
free(buffer);
return (-1);
}
if (format[i] == 'c' && str[0] == '\0')
{
len = check_buffer_overflow(buffer, len);
buffer[len++] = '\0';
total_len++;
}
j = 0;
while (str[j] != '\0')
{
len = check_buffer_overflow(buffer, len);
buffer[len++] = str[j];
total_len++;
j++;
}
free(str);
}
} i++;
}
}
write_buffer(buffer, len, list);
return (total_len);
}