-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
65 lines (60 loc) · 1.77 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
64
65
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amait-ou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/17 10:11:52 by amait-ou #+# #+# */
/* Updated: 2023/01/24 00:07:54 by amait-ou ### ########.fr */
/* */
/* ************************************************************************** */
#include "./superlib.h"
static int helper(const char *s, va_list args)
{
int i;
i = 0;
if (*s == 'c')
i = ft_putchar(va_arg(args, int));
else if (*s == 's')
i = ft_putstr(va_arg(args, char *));
else if (*s == '%')
i = ft_putchar('%');
else if (*s == 'x')
i = ft_printhex(va_arg(args, t_ui), "0123456789abcdef");
else if (*s == 'X')
i = ft_printhex(va_arg(args, t_ui), "0123456789ABCDEF");
else if (*s == 'd' || *s == 'i')
i = ft_putsigned(va_arg(args, int));
else if (*s == 'u')
i = ft_putunsigned(va_arg(args, t_ui));
else if (*s == 'p')
{
i = ft_putstr("0x");
i += ft_printhex(va_arg(args, t_ul), "0123456789abcdef");
}
return (i);
}
int ft_printf(const char *s, ...)
{
va_list args;
int l;
va_start(args, s);
l = 0;
while (*s)
{
if (*s == '%')
{
++s;
l += helper(s, args);
++s;
}
else
{
l += ft_putchar(*s);
++s;
}
}
va_end(args);
return (l);
}