-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
64 lines (58 loc) · 2 KB
/
ft_printf.c
File metadata and controls
64 lines (58 loc) · 2 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: lsasse <lsasse@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/17 15:05:17 by lsasse #+# #+# */
/* Updated: 2024/01/01 21:40:20 by lsasse ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar(char c)
{
return (write(1, &c, 1));
}
int ft_putformat(const char **format, va_list ap)
{
int count;
count = 0;
++*format;
if (**format == 'c')
count = ft_putchar((char)va_arg(ap, int));
else if (**format == 's')
count = ft_putstr((char *)va_arg(ap, char *));
else if (**format == 'i' || **format == 'd')
count = ft_putnbr((int)va_arg(ap, int));
else if (**format == 'u')
count = ft_putunbr((long long unsigned int)va_arg(ap, unsigned int));
else if (**format == 'x')
count = ft_puthex((long long unsigned int)va_arg(ap, unsigned int), 0);
else if (**format == 'X')
count = ft_puthex((long long unsigned int)va_arg(ap, unsigned int), 1);
else if (**format == 'p')
count = ft_putptr((void *)va_arg(ap, void *));
else if (**format == '%')
count = ft_putchar('%');
++*format;
return (count);
}
int ft_printf(const char *format, ...)
{
int count;
va_list ap;
count = 0;
if (!format)
return (-1);
va_start(ap, format);
while (*format)
{
if (*format != '%')
count += ft_putchar(*format++);
else
count += ft_putformat(&format, ap);
}
va_end(ap);
return (count);
}