-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
56 lines (52 loc) · 1.33 KB
/
Copy path_printf.c
File metadata and controls
56 lines (52 loc) · 1.33 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
#include "main.h"
/**
* _printf - A custom implementation of the printf function.
* @format: The format string, which can contain zero or more directives
* (like %s, %d, %c, etc.) specifying the required conversions.
*
* Description:
* This function prints its input arguments following a format string provided
* in 'format'. It can handle various format specifiers and their respective
* flags, widths, precisions, and modifiers.
*
* Return: Number of bytes printed to the output.
*/
int _printf(const char *format, ...)
{
int sum = 0;
va_list ap;
char *p, *start;
params_t params = PARAMS_INIT;
va_start(ap, format);
if (!format || (format[0] == '%' && !format[1]))
return (-1);
if (format[0] == '%' && format[1] == ' ' && !format[2])
return (-1);
for (p = (char *)format; *p; p++)
{
init_params(¶ms, ap);
if (*p != '%')
{
sum += _putchar(*p);
continue;
}
start = p;
p++;
while (get_flag(p, ¶ms)) /* while char at p is flag char */
{
p++; /* next char */
}
p = get_width(p, ¶ms, ap);
p = get_precision(p, ¶ms, ap);
if (get_modifier(p, ¶ms))
p++;
if (!get_specifier(p))
sum += print_from_to(start, p,
params.l_modifier || params.h_modifier ? p - 1 : 0);
else
sum += get_print_func(p, ap, ¶ms);
}
_putchar(BUF_FLUSH);
va_end(ap);
return (sum);
}