-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
74 lines (67 loc) · 1.9 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
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: melkholy <melkholy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/25 13:51:56 by melkholy #+# #+# */
/* Updated: 2022/09/12 20:22:34 by melkholy ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_check_specifier(const char *fmt, va_list arg)
{
int len;
if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u' || *fmt == 'x' || *fmt == 'X')
len = ft_print_d_u_x(*fmt, arg);
else if (*fmt == 'c')
len = ft_character(arg);
else if (*fmt == 's')
len = ft_string(arg);
else if (*fmt == 'p')
len = ft_pointer(*fmt, va_arg(arg, size_t));
else if (*fmt == '%')
len = write(1, "%", 1);
else
len = 0;
return (len);
}
int ft_check_print(const char *fmt, va_list arg)
{
int count;
int pchr;
count = 0;
pchr = 0;
while (fmt[count])
{
if (fmt[count] == '%')
{
count ++;
pchr += ft_check_specifier(&fmt[count], arg);
}
else
pchr += write(1, &fmt[count], 1);
count ++;
}
return (pchr);
}
int ft_printf(const char *fmt, ...)
{
va_list ap;
int pchr;
if (!fmt)
{
ft_putstr_fd("(null)", 1);
return (6);
}
if (!ft_strchr(fmt, '%'))
{
ft_putstr_fd((char *)fmt, 1);
return ((int)ft_strlen(fmt));
}
va_start(ap, fmt);
pchr = ft_check_print(fmt, ap);
va_end(ap);
return (pchr);
}