-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
executable file
·56 lines (52 loc) · 1.2 KB
/
ft_printf.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
#include "ft_printf.h"
int ft_printf(const char *format, ...)
{
int flag;
int count;
va_list params;
flag = 0;
count = 0;
va_start(params, format);
while (*format != '\0')
{
if (*format == '%')
{
format++;
flag = formatspec(format, params);
}
else
flag = write(1, format, 1);
if (flag == -1)
return (-1);
count += flag;
if (*format != '\0')
format++;
}
va_end(params);
return (count);
}
int formatspec(const char *format, va_list params)
{
if (*format == 'c')
return (ft_putchar(va_arg(params, int)));
else if (*format == 's')
return (ft_putstr(va_arg(params, char *)));
else if ((*format == 'd') || (*format == 'i'))
return (ft_putnbr("0123456789", (va_arg(params, int))));
else if (*format == 'x')
return (ft_puthex("0123456789abcdef", \
(va_arg(params, unsigned int))));
else if (*format == 'X')
return (ft_puthex("0123456789ABCDEF", \
(va_arg(params, unsigned int))));
else if (*format == 'p')
return (ft_putptr(va_arg(params, void *)));
else if (*format == 'u')
return (ft_putuint("0123456789", va_arg(params, unsigned int)));
else if (*format == '%')
return (ft_putchar('%'));
else if (*format == '\0')
return (0);
else
return (-1);
}