-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
64 lines (60 loc) · 1.89 KB
/
ft_printf.c
File metadata and controls
64 lines (60 loc) · 1.89 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iuslu <iuslu@student.42kocaeli.com.tr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/13 15:22:47 by iuslu #+# #+# */
/* Updated: 2026/02/18 19:31:17 by iuslu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdarg.h>
#include <unistd.h>
static int ft_fcontrol(const char *s, va_list args)
{
if (*(s + 1) == 'c')
return (ft_putchar(va_arg(args, int)));
else if (*(s + 1) == 's')
return (ft_putstr(va_arg(args, char *)));
else if (*(s + 1) == 'p')
return (ft_putptr(va_arg(args, unsigned long)));
else if (*(s + 1) == 'd' || *(s + 1) == 'i')
return (ft_putnbr(va_arg(args, int)));
else if (*(s + 1) == 'u')
return (ft_putnbr(va_arg(args, unsigned int)));
else if (*(s + 1) == 'x' || *(s + 1) == 'X')
return (ft_puthex(va_arg(args, unsigned int), *(s + 1)));
else if (*(s + 1) == '%')
return (write(1, "%", 1));
else
return (write(1, s, 2));
}
int ft_printf(const char *s, ...)
{
va_list args;
int check;
check = 0;
if (!s)
return (-1);
va_start(args, s);
while (*s)
{
if (*s == '%' && *(s + 1))
{
check += ft_fcontrol(s, args);
s += 2;
}
else if (*s == '%' && !*(s + 1))
s++;
else
{
write(1, s, 1);
s++;
check++;
}
}
va_end(args);
return (check);
}