-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprintf.c
More file actions
90 lines (84 loc) · 2.05 KB
/
printf.c
File metadata and controls
90 lines (84 loc) · 2.05 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "holberton.h"
int write_print(mk_buffer container, va_list args);
/**
* _printf - Creates a buffer and writes that buffer to standard output
* @format: the string to be printed, may contain conversion specifiers
* which placehold for other data types to be printed
*
* Return: The number of characters printed.
*/
int _printf(const char *format, ...)
{
va_list args;
mk_buffer container;
check_null(format);
container = create_buffer(container);
va_start(args, format);
while (*format)
{
if (*format == '%' && get_format(format + 1))
{
if (!(get_format(format + 1)))
{
container = add_buff(container, args, format, 0);
format++;
continue;
}
while (*(format + 1) == ' ')
format++;
if (*(format + 1) == '\0')
{
format++;
continue;
}
else if (*(format + 1) == '\n' && *format == '%')
{
container = add_buff(container, args, 0, '%');
format++;
continue;
}
if (*(format + 1) && !(get_format(format + 1)))
{
container = add_buff(container, args, 0, '%');
container = add_buff(container, args, format, 0);
format++;
continue;
}
else if (*format == ' ')
container = add_buff(container, args, format, 0);
format++;
container = get_format(format)(container, args);
}
else if (*format == '%' && *(format + 1) == '\0')
{
write(1, container.start, container.size);
free(container.start);
va_end(args);
return (-1);
}
else
{
if (*(format + 1) == '%' && *format == '%')
format++;
*container.box = *format;
container.size += 1;
}
container.box++;
format++;
}
return (write_print(container, args));
}
/**
* write_print - Writes and frees the buffer to standard output
* @container: the string to be printed, may contain conversion specifiers
* which placehold for other data types to be printed
* @args: the args
* Return: The number of characters printed.
*/
int write_print(mk_buffer container, va_list args)
{
write(1, container.start, container.size);
free(container.start);
va_end(args);
return (container.size);
}