-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
66 lines (61 loc) · 1.99 KB
/
ft_printf.c
File metadata and controls
66 lines (61 loc) · 1.99 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adores <adores@student.42lisboa.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 11:10:01 by adores #+# #+# */
/* Updated: 2025/05/06 14:37:40 by adores ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int print_format(char spec, va_list ap)
{
int count;
count = 0;
if (spec == 'c')
count += ft_printchar(va_arg(ap, int));
else if (spec == 'd')
count += ft_printnbr(va_arg(ap, int));
else if (spec == 'i')
count += ft_printnbr(va_arg(ap, int));
else if (spec == 'u')
count += ft_unsputnbr(va_arg(ap, unsigned int));
else if (spec == 'x')
count += ft_hexaputnbr(va_arg(ap, unsigned int), 'x');
else if (spec == 'X')
count += ft_hexaputnbr(va_arg(ap, unsigned int), 'X');
else if (spec == 's')
count += ft_printstr(va_arg(ap, char *));
else if (spec == 'p')
count += ft_checkp(va_arg(ap, void *));
else if (spec == '%')
count += ft_printchar('%');
return (count);
}
int ft_printf(const char *format, ...)
{
va_list list;
int count;
int i;
if (!format || write(1, 0, 0) == -1)
return (-1);
count = 0;
i = 0;
va_start(list, format);
while (format[i])
{
if (format[i] == '%')
{
if (format[i + 1] == '\0')
return (va_end(list), -1);
count += print_format(format[++i], list);
}
else
count += ft_printchar(format[i]);
i++;
}
va_end(list);
return (count);
}