-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
63 lines (60 loc) · 2.07 KB
/
Copy pathft_printf.c
File metadata and controls
63 lines (60 loc) · 2.07 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yelallam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/21 09:50:39 by yelallam #+# #+# */
/* Updated: 2025/12/01 18:59:14 by yelallam ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int handle_format(const char *str, int i, va_list ap)
{
if (str[i + 1] == 'c')
return (ft_putchar_fd((char)va_arg(ap, int), 1));
else if (str[i + 1] && str[i + 1] == 's')
return (ft_putstr_fd(va_arg(ap, char *), 1));
else if (str[i + 1] == 'd' || str[i + 1] == 'i')
return (ft_putnbr_fd(va_arg(ap, int), 1));
else if (str[i + 1] == '%')
return (ft_putchar_fd('%', 1));
else if (str[i + 1] == 'u')
return (ft_put_un_nbr_fd((unsigned int)va_arg(ap, unsigned int), 1));
else if (str[i + 1] == 'p')
return (ft_putptr((uintptr_t)va_arg(ap, void *)));
else if (str[i + 1] == 'x')
return (ft_puthexa_low((unsigned int)va_arg(ap, unsigned int)));
else if (str[i + 1] == 'X')
return (ft_puthexa_upp((unsigned int)va_arg(ap, unsigned int)));
else
return (-1);
}
int ft_printf(const char *str, ...)
{
va_list ap;
int i, (count), (tmp);
va_start(ap, str);
i = 0;
count = 0;
if (str == NULL)
return (-1);
if (str[i] == '\0')
return (0);
while (str[i])
{
if (str[i] == '%')
{
tmp = handle_format(str, i, ap);
if (tmp == -1)
return (-1);
count += tmp;
i = i + 2;
}
else
count += ft_putchar_fd(str[i++], 1);
}
va_end(ap);
return (count);
}