-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
68 lines (64 loc) · 2.12 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
66
67
68
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tunsal <tunsal@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/20 16:20:53 by tunsal #+# #+# */
/* Updated: 2023/11/11 20:18:18 by tunsal ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
Handle format specifiers and return number of characters printed onto screen.
Return 0 (defined as SUCCESS) if no errors, else -1 (defined as FAILURE).
*/
static int handle_specifiers(const char *fmt, va_list *valst)
{
if (*fmt == 'c')
return (ft_putchar((char) va_arg(*valst, int)));
else if (*fmt == 's')
return (ft_putstr(va_arg(*valst, char *)));
else if (*fmt == 'p')
return (ft_putunbr_ptr(va_arg(*valst, size_t), 1));
else if (*fmt == 'd' || *fmt == 'i')
return (ft_putnbr(va_arg(*valst, int)));
else if (*fmt == 'u')
return (ft_putunbr(va_arg(*valst, unsigned int)));
else if (*fmt == 'x')
return (ft_putunbr_hex(va_arg(*valst, unsigned int), 0));
else if (*fmt == 'X')
return (ft_putunbr_hex(va_arg(*valst, unsigned int), 1));
else if (*fmt == '%')
return (ft_putchar('%'));
else
return (-1);
}
int ft_printf(const char *fmt, ...)
{
va_list valist;
int print_count;
int ret;
va_start(valist, fmt);
print_count = 0;
while (*fmt != '\0')
{
if (*fmt == '%')
{
ret = handle_specifiers(++fmt, &valist);
if (ret < 0)
return (-1);
print_count += ret;
}
else
{
if (ft_putchar(*fmt) != 1)
return (-1);
++print_count;
}
++fmt;
}
va_end(valist);
return (print_count);
}