-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
65 lines (60 loc) · 2.08 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: aelkheta <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 15:16:09 by aelkheta #+# #+# */
/* Updated: 2023/12/01 18:05:21 by aelkheta ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int check_specifier(char format, va_list args)
{
int counter;
counter = 0;
if (format == 'd' || format == 'i')
counter += ft_putnbr(va_arg(args, int));
else if (format == 'u')
counter += ft_putnbr_unsigned(va_arg(args, unsigned int));
else if (format == 'c')
counter += ft_print_char(va_arg(args, int));
else if (format == 's')
counter += ft_print_str(va_arg(args, char *));
else if (format == 'x')
counter += ft_print_hex_low(va_arg(args, unsigned int));
else if (format == 'X')
counter += ft_print_hex_upp(va_arg(args, unsigned int));
else if (format == 'p')
counter += ft_print_address(va_arg(args, void *));
else
counter += ft_print_char(format);
return (counter);
}
int ft_printf(const char *format, ...)
{
int i;
int counter;
va_list args;
i = 0;
counter = 0;
va_start(args, format);
if (!format || (*(format + 1) && *format == '%' && *(format + 1) == '\0'))
return (-1);
while (format[i])
{
if (format[i] == '%' && format[i + 1] == '%')
{
counter += ft_print_char('%');
i++;
}
else if (format[i] == '%')
counter += check_specifier(format[++i], args);
else
counter += write(1, &format[i], 1);
i++;
}
va_end(args);
return (counter);
}