-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
61 lines (56 loc) · 1.89 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gpuscedd <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/25 16:03:37 by gpuscedd #+# #+# */
/* Updated: 2024/01/29 12:44:26 by gpuscedd ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_format(va_list args, const char format)
{
int len;
len = 0;
if (format == '%')
len += ft_putchar_fd('%', 1);
else if (format == 'c')
len += ft_putchar_fd(va_arg(args, int), 1);
else if (format == 's')
len += ft_putstr_fd(va_arg(args, char *), 1);
else if (format == 'd' || format == 'i')
len += ft_putnbr_fd(va_arg(args, int), 1);
else if (format == 'p')
len += ft_putptr(va_arg(args, unsigned long long));
else if (format == 'u')
len += ft_putnbr_fd(va_arg(args, unsigned int), 1);
else if (format == 'X')
len += ft_putnbr_base(va_arg(args, unsigned int), "0123456789ABCDEF");
else if (format == 'x')
len += ft_putnbr_base(va_arg(args, unsigned int), "0123456789abcdef");
return (len);
}
int ft_printf(const char *str, ...)
{
va_list args;
int len;
va_start(args, str);
len = 0;
while (*str)
{
if (*str == '%')
{
len += ft_format(args, *(++str));
str++;
}
else
{
len += ft_putchar_fd(*str, 1);
str++;
}
}
va_end (args);
return (len);
}