-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
64 lines (59 loc) · 1.91 KB
/
Copy pathft_printf.c
File metadata and controls
64 lines (59 loc) · 1.91 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: isel-azz <isel-azz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 19:49:34 by isel-azz #+# #+# */
/* Updated: 2023/12/09 21:28:01 by isel-azz ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int print_format(char spec, va_list ap)
{
int count;
count = 0;
if (spec == 'c')
count += print_char(va_arg(ap, int));
else if (spec == 's')
count += print_str(va_arg(ap, char *));
else if (spec == 'd' || spec == 'i')
count += print_number(va_arg(ap, int));
else if (spec == 'u')
count += print_unsigned(va_arg(ap, unsigned int));
else if (spec == 'x')
count += itohex(va_arg(ap, unsigned int), spec);
else if (spec == 'X')
count += itohex(va_arg(ap, unsigned int), spec);
else if (spec == 'p')
count += printadress(va_arg(ap, unsigned long));
else if (spec == '%')
count += print_char('%');
else if (spec)
count += print_char(spec);
return (count);
}
int ft_printf(const char *format, ...)
{
int count;
va_list ap;
count = 0;
if (write(1, 0, 0) < 0)
return (-1);
va_start(ap, format);
while (*format != '\0')
{
if (*format == '%')
{
format++;
count += print_format(*format, ap);
}
else
count += write(1, format, 1);
if (*format)
format++;
}
va_end(ap);
return (count);
}