-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
93 lines (86 loc) · 2.4 KB
/
ft_printf.c
File metadata and controls
93 lines (86 loc) · 2.4 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbarba-v <dbarba-v@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/10 09:28:08 by dbarba-v #+# #+# */
/* Updated: 2025/11/06 16:11:25 by dbarba-v ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdarg.h>
#include <unistd.h>
static int ft_parse(const char *format, va_list argument, int fd)
{
int i;
i = 0;
if (*(format + 1) == 'c')
i = ft_putchar_fd(va_arg(argument, int), fd);
else if (*(format + 1) == 's')
i = ft_putstr_fd(va_arg(argument, char *), fd);
else if (*(format + 1) == 'p')
i = ft_putptr_fd(va_arg(argument, void *), fd);
else if (*(format + 1) == 'd' || *(format + 1) == 'i')
i = ft_putnbr_fd(va_arg(argument, int), fd);
else if (*(format + 1) == 'u')
i = ft_putuns_fd(va_arg(argument, unsigned int), fd);
else if (*(format + 1) == 'x')
i = ft_puthexl_fd(va_arg(argument, unsigned int), fd);
else if (*(format + 1) == 'X')
i = ft_puthexu_fd(va_arg(argument, unsigned int), fd);
else if (*(format + 1) == '%')
i = ft_putchar_fd('%', fd);
else
return (i);
return (i);
}
int ft_printf(const char *format, ...)
{
va_list arguments;
int i;
if (!format)
return (0);
va_start(arguments, format);
i = 0;
while (*format)
{
if (*format == '%')
{
i += ft_parse(format, arguments, STDOUT_FILENO);
format += 2;
}
else
{
i += ft_putchar_fd(*format, 1);
format++;
}
}
va_end(arguments);
return (i);
}
int ft_dprintf(int fd, const char *format, ...)
{
va_list arguments;
int i;
if (!format)
return (0);
va_start(arguments, format);
i = 0;
while (*format)
{
if (*format == '%')
{
i += ft_parse(format, arguments, fd);
format += 2;
}
else
{
i += ft_putchar_fd(*format, fd);
format++;
}
}
va_end(arguments);
return (i);
}