-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
77 lines (70 loc) · 2.12 KB
/
ft_printf.c
File metadata and controls
77 lines (70 loc) · 2.12 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mkhellou < [email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 11:52:58 by mkhellou #+# #+# */
/* Updated: 2022/11/11 09:08:52 by mkhellou ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_puthex(unsigned int i, int *len, char *base)
{
if (i > 15)
ft_puthex(i / 16, len, base);
ft_putchar(base[i % 16], len);
}
static void ft_putsubptr(unsigned long long i, int *len, char *base)
{
if (i > 15)
ft_putsubptr(i / 16, len, base);
ft_putchar(base[i % 16], len);
}
void ft_putptr(unsigned long long i, int *len)
{
ft_putstr("0x", len);
ft_putsubptr(i, len, "0123456789abcdef");
}
static void ft_format(char c, va_list ptr, int *len)
{
if (c == 'c')
ft_putchar(va_arg(ptr, int), len);
else if (c == 's')
ft_putstr(va_arg(ptr, char *), len);
else if (c == 'i' || c == 'd')
ft_putnbr(va_arg(ptr, int), len);
else if (c == 'u')
ft_putuns(va_arg(ptr, unsigned int), len);
else if (c == 'x')
ft_puthex(va_arg(ptr, unsigned int), len, "0123456789abcdef");
else if (c == 'X')
ft_puthex(va_arg(ptr, unsigned int), len, "0123456789ABCDEF");
else if (c == 'p')
ft_putptr(va_arg(ptr, unsigned long long), len);
else
ft_putchar(c, len);
}
int ft_printf(const char *str, ...)
{
va_list ptr;
int i;
int len;
i = 0;
len = 0;
va_start(ptr, str);
while (str[i])
{
if (str[i] != '%')
ft_putchar(str[i], &len);
else
{
i++;
ft_format(str[i], ptr, &len);
}
i++;
}
va_end(ptr);
return (len);
}