-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
58 lines (54 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
57
58
#include "ft_printf.h"
static size_t checkformat(const char *c, size_t *i, va_list *argptr)
{
char format;
*i = *i + 1;
format = *(c + 1);
if (format == 'c')
return (ft_putchar((char) va_arg(*argptr, int)));
else if (format == 's')
return (ft_putstr(va_arg(*argptr, const char *)));
else if (format == 'p')
return (ft_putaddress(va_arg(*argptr, void *)));
else if (format == 'd' || format == 'i')
return (ft_putnbr(va_arg(*argptr, int)));
else if (format == 'u')
return (ft_putunbr(va_arg(*argptr, unsigned int)));
else if (format == 'x')
return (ft_puthex_l(va_arg(*argptr, int)));
else if (format == 'X')
return (ft_puthex_u(va_arg(*argptr, int)));
else if (format == '%')
return (ft_putchar('%'));
else if (format && (format + 1))
return (ft_putchar(*(c)) + ft_putchar(format));
else
*i = *i - 1;
return (-1);
}
int ft_printf(const char *str, ...)
{
int len;
int tmp;
va_list argptr;
size_t i;
if (!str)
return (-1);
len = 0;
va_start(argptr, str);
i = 0;
while (str[i])
{
if (str[i] == '%')
tmp = checkformat(str + i, &i, &argptr);
else
tmp = ft_putchar(str[i]);
if (tmp >= 0)
len += tmp;
else
len = tmp;
i++;
}
va_end(argptr);
return (len);
}