-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
67 lines (61 loc) · 1.94 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tpiras <tpiras@student.42roma.it> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 13:19:56 by tpiras #+# #+# */
/* Updated: 2023/02/24 14:17:19 by tpiras ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <unistd.h>
#include <stdarg.h>
int ft_printchar(char c)
{
write(1, &c, 1);
return (1);
}
int check_formats(va_list args, const char format)
{
int len_print;
len_print = 0;
if (format == 'c')
len_print += ft_printchar(va_arg(args, int));
if (format == 's')
len_print += ft_printstr(va_arg(args, char *));
if (format == 'p')
len_print += ft_printpoint(va_arg(args, unsigned long long));
if (format == 'i' || format == 'd')
len_print += ft_printnbr(va_arg(args, int));
if (format == 'u')
len_print += ft_printunbr(va_arg(args, unsigned int));
if (format == 'x' || format == 'X')
len_print += ft_printhex(va_arg(args, unsigned int), format);
if (format == '%')
len_print += ft_printperc();
return (len_print);
}
int ft_printf(const char *str, ...)
{
int i;
va_list args;
int len_print;
i = 0;
len_print = 0;
va_start(args, str);
while (str[i])
{
if (str[i] == '%')
{
len_print += check_formats(args, str[i + 1]);
i++;
}
else
len_print += ft_printchar(str[i]);
i++;
}
va_end(args);
return (len_print);
}