This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
69 lines (61 loc) · 1.86 KB
/
ft_printf.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minjungk <[email protected].> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/17 04:45:14 by minjungk #+# #+# */
/* Updated: 2024/06/13 04:52:38 by minjungk ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "ft_printf_util.h"
int ft_printf(const char *format, ...)
{
int total;
va_list ap;
va_start(ap, format);
total = ft_vprintf(format, ap);
va_end(ap);
return (total);
}
int ft_dprintf(int fd, const char *format, ...)
{
int total;
va_list ap;
va_start(ap, format);
total = ft_vdprintf(fd, format, ap);
va_end(ap);
return (total);
}
int ft_vprintf(const char *format, va_list ap)
{
return (ft_vdprintf(STDOUT_FILENO, format, ap));
}
int ft_vdprintf(int fd, const char *format, va_list ap)
{
t_token *token;
size_t len;
int ret;
int total;
len = 0;
total = 0;
while (format && format[len] != '\0')
{
ret = -1;
token = ft_make_token(format + len);
if (0 == ft_parse_token(token, ap) && token->out)
{
if (token->width == 0)
token->width = ft_strlen(token->out);
ret = write(fd, token->out, token->width);
total += ret;
len += ft_strlen(token->in);
}
ft_free_token(token);
if (ret < 0)
return (0);
}
return (total);
}