-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
59 lines (55 loc) · 1.72 KB
/
ft_printf.c
File metadata and controls
59 lines (55 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tjeunet <tjeunet@student.42barcelo> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/21 16:14:58 by tjeunet #+# #+# */
/* Updated: 2023/03/21 16:22:05 by tjeunet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void check_format(char const *str, va_list arg, int *arg_len)
{
if (*str == 'c')
return (ft_putchar(va_arg(arg, int), arg_len));
if (*str == '%')
*arg_len += write(1, "%", 1);
if (*str == 'd' || *str == 'i')
print_int(arg, arg_len);
if (*str == 'u')
print_uint(arg, arg_len);
if (*str == 'x' || *str == 'X')
print_hexa(arg, arg_len,*str);
if (*str == 'p')
print_address_hexa(arg, arg_len);
if (*str == 's')
print_str(arg, arg_len);
}
int ft_printf(const char *format, ...)
{
va_list args;
int arg_len;
va_start(args, format);
arg_len = 0;
while (*format)
{
if (*format == '%')
{
format++;
check_format(format, args, &arg_len);
if (arg_len == -1)
return (-1);
}
else
{
if (write(1, format, 1) != 1)
return (-1);
arg_len ++;
}
format++;
}
va_end(args);
return (arg_len);
}