-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
63 lines (59 loc) · 1.76 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
59
60
61
62
63
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mriant <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 10:39:40 by mriant #+# #+# */
/* Updated: 2022/01/19 14:17:58 by mriant ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
int ft_printconv(va_list ap, char c)
{
if (c == 'c')
return (ft_putchar_int(va_arg(ap, int)));
if (c == 's')
return (ft_putstr_int(va_arg(ap, char *)));
if (c == 'd' || c == 'i')
return (ft_putdec(va_arg(ap, int)));
if (c == 'p')
return (ft_putpoint((intptr_t) va_arg(ap, void *)));
if (c == 'u')
return (ft_putuint(va_arg(ap, unsigned int)));
if (c == 'x')
return (ft_putx(va_arg(ap, unsigned int)));
if (c == 'X')
return (ft_putxmaj(va_arg(ap, unsigned int)));
if (c == '%')
return (ft_putchar_int('%'));
else
{
ft_putchar_int('%');
ft_putchar_int(c);
return (2);
}
}
int ft_printf(const char *s, ...)
{
int i;
int total;
va_list ap;
i = 0;
total = 0;
va_start(ap, s);
while (s[i])
{
if (s[i] == '%')
{
i ++;
total += ft_printconv(ap, s[i]);
}
else
total += write (1, &s[i], 1);
i ++;
}
va_end(ap);
return (total);
}