-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
85 lines (78 loc) · 2.21 KB
/
Copy pathft_printf.c
File metadata and controls
85 lines (78 loc) · 2.21 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbartosi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/17 12:40:23 by jbartosi #+# #+# */
/* Updated: 2023/01/29 16:15:49 by jbartosi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_num_len(unsigned long nb, int divider)
{
int i;
i = 0;
if (nb == 0)
return (1);
while (nb > 0)
{
i++;
nb /= divider;
}
return (i);
}
int ft_print(t_format format, va_list args)
{
unsigned long ptr;
int printed;
int malloced;
printed = 0;
malloced = 0;
if (format.specifier == 'c' || format.specifier == '%')
return (ft_print_char(format, args));
if (format.specifier == 's')
return (ft_print_str(format, va_arg(args, char *), printed, malloced));
if (format.specifier == 'd' || format.specifier == 'i'
|| format.specifier == 'u')
return (ft_format_nbr(format, args));
if (format.specifier == 'x' || format.specifier == 'X')
return (ft_print_hex(format, args));
if (format.specifier == 'p')
{
ptr = va_arg(args, unsigned long);
if (ptr)
return (ft_print_mem(format, ptr));
else
return (ft_print_str(format, "(nil)", 0, 0));
}
return (-1);
}
int ft_printf(const char *str, ...)
{
int printed;
va_list args;
char *save;
printed = 0;
va_start(args, str);
while (*str)
{
if (*str == '%')
{
save = (char *)str;
if (*(++str))
printed += ft_format((char *)str, args);
while (*str && !ft_strchr(SPECIFIERS, *str))
str++;
if (!(*str))
str = save;
}
else
printed += ft_printchar(*str);
if (*str)
str++;
}
va_end(args);
return (printed);
}