-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf2.c
79 lines (68 loc) · 2.09 KB
/
ft_printf2.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
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smbaabu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/13 22:10:07 by smbaabu #+# #+# */
/* Updated: 2019/07/14 12:28:55 by smbaabu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_fprintf(FILE *restrict stream, const char *restrict format, ...)
{
va_list args;
char buf[BUF_SIZE];
int count;
va_start(args, format);
count = ft_print(buf, args, format);
va_end(args);
return (write(fileno(stream), buf, count));
}
int ft_sprintf(char *restrict str, const char *restrict format, ...)
{
va_list args;
int count;
va_start(args, format);
count = ft_print(str, args, format);
str[(count > MAX_INT ? MAX_INT : count) - 1] = 0;
va_end(args);
return (count);
}
int ft_snprintf(char *restrict str, size_t size,
const char *restrict format, ...)
{
va_list args;
int count;
va_start(args, format);
count = ft_print(str, args, format);
str[size - 1] = 0;
va_end(args);
return (count);
}
int ft_asprintf(char **ret, const char *format, ...)
{
va_list args;
int count;
*ret = malloc((BUF_SIZE) * sizeof(char));
if (!*ret)
{
*ret = NULL;
return (-1);
}
va_start(args, format);
count = ft_print(*ret, args, format);
va_end(args);
return (count);
}
int ft_dprintf(int fd, const char *restrict format, ...)
{
va_list args;
char buf[BUF_SIZE];
int count;
va_start(args, format);
count = ft_print(buf, args, format);
va_end(args);
return (write(fd, buf, count));
}