Repository navigation
Expand file tree
/
Copy pathft_printf.c
More file actions
104 lines (94 loc) · 2.22 KB
/
Copy pathft_printf.c
File metadata and controls
104 lines (94 loc) · 2.22 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vsivanat <vsivanat@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/22 15:51:50 by vsivanat #+# #+# */
/* Updated: 2023/11/02 17:01:25 by vsivanat ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int one_character(int c)
{
return (write(1, &c, 1));
}
int string(char *s)
{
int i;
i = 0;
if (!s)
{
if (write(1, "(null)", 6) == -1)
return (-1);
return (6);
}
else
{
while (s[i] != '\0')
i++;
if (write(1, s, i) == -1)
return (-1);
}
return (i);
}
int prozent(const char *s, va_list args)
{
int count;
count = 0;
s += 1;
if ((*s) == 'c')
count = one_character(va_arg(args, int));
else if ((*s) == 's')
count = string(va_arg(args, char *));
else if ((*s) == 'p')
count = pointer(va_arg(args, void *));
else if ((*s) == 'd' || (*s) == 'i')
count = integer(va_arg(args, int));
else if ((*s) == 'u')
count = putnbr(va_arg(args, unsigned int));
else if ((*s) == 'X')
count = convert(va_arg(args, int), 'X');
else if ((*s) == 'x')
count = convert(va_arg(args, int), 'x');
else if ((*s) == 37)
count = write(1, "%", 1);
else
return (-1);
return (count);
}
size_t ft_loop(const char *s, va_list *args)
{
int count;
int i;
count = 0;
while (*s)
{
if (*s == '%' && s[1])
{
i = prozent(s, *args);
s += 1;
}
else
{
i = one_character(*s);
}
if (i == -1)
return (-1);
count += i;
i = 0;
s += 1;
}
return (count);
}
int ft_printf(const char *input, ...)
{
va_list args;
int i;
i = 0;
va_start(args, input);
i = ft_loop(input, &args);
va_end(args);
return (i);
}