-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
63 lines (59 loc) · 1.9 KB
/
ft_printf.c
File metadata and controls
63 lines (59 loc) · 1.9 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: nnuno-ca <nnuno-ca@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/13 12:24:06 by nnuno-ca #+# #+# */
/* Updated: 2022/11/16 22:50:21 by nnuno-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/ft_printf.h"
static int exec_conversion(char spec_ltr, va_list args)
{
if (spec_ltr == '%')
return (write(1, "%", 1));
else if (spec_ltr == 'c')
{
ft_putchar_fd(va_arg(args, int), 1);
return (1);
}
else if (spec_ltr == 's')
return (prt_str(va_arg(args, char *)));
else if (spec_ltr == 'p')
return (prt_ptr(va_arg(args, void *)));
else if (spec_ltr == 'd' || spec_ltr == 'i')
return (prt_int(va_arg(args, int)));
else if (spec_ltr == 'u')
return (prt_unsigned(va_arg(args, unsigned int)));
else if (spec_ltr == 'x')
return (prt_hexa(va_arg(args, ssize_t), false));
else if (spec_ltr == 'X')
return (prt_hexa(va_arg(args, ssize_t), true));
return (0);
}
int ft_printf(const char *__format, ...)
{
int i;
int len;
va_list args;
if (!__format)
return (0);
i = 0;
len = 0;
va_start(args, __format);
while (__format[i])
{
if (__format[i] == '%')
{
i++;
len += exec_conversion(__format[i], args);
}
else
len += write(1, &__format[i], 1);
i++;
}
va_end(args);
return (len);
}