- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
Latest commit
52 lines (50 loc) · 1.45 KB
/
Copy path_printf.c
File metadata and controls
52 lines (50 loc) · 1.45 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
#include"main.h"
/**
* _printf - custom printf function
* @format: the output string format
* @...: variable arguments to be printed
* Description: prints all passed arguments in a specific format
* just like the standard printf does.
* Return: integer - the count of the printed characters (-1 for error)
*/
int_printf(constchar*format, ...)
{
intcount=0;
va_listargs;
va_start(args, format); /* initializes the va_list */
if (!format) /* check if format is valid */
return (-1);
if (!format[0]) /* check if format is empty */
return (0);
while (*format) /* go through every format character */
{
if (*format=='%') /* placeholder is encountred */
{
format++; /* move pointer to the next character */
if (*format=='\0'&&count==0) /* format == "%" case */
return (-1);
if (*format=='%') /* other % encountered */
count+=_putchar('%');
/* call the right handler function */
elseif (get_handler_func(*format) !=NULL)
count+=get_handler_func(*format)(args);
else/* none specified placeholder OR end of format string \0 */
{
/* in both cases print % */
count+=_putchar('%');
if (*format!='\0') /* print the none specified placeholder */
count+=_putchar(*format);
else/* exit loop if format string equal null termination */
break;
}
format++;
}
else/* not a placeholder */
{
count+=_putchar(*format);
format++;
}
}
va_end(args);
return (count);
}