-
Notifications
You must be signed in to change notification settings - Fork 0
/
heredoc.c
80 lines (72 loc) · 2.02 KB
/
heredoc.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
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkassand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 01:29:07 by jkassand #+# #+# */
/* Updated: 2021/11/27 01:29:10 by jkassand ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int ft_strcmp_h(const char *s1, const char *s2)
{
int res;
res = 0;
while ((*s1 || *s2) && !res)
res = (unsigned char)*(s1++) - (unsigned char)*(s2++);
return (res);
}
static void gnl(char **line, t_arg *arg)
{
char *end;
char *tmp;
*line = ft_strdup("\0");
end = ft_strdup("\0\0");
if (!*line || !end)
ft_exit(12, "malloc", arg);
while (*end != '\n')
{
read(0, end, 1);
if (*end == '\n')
break ;
tmp = ft_strjoin(*line, end);
if (!tmp)
{
free(end);
ft_exit(12, "malloc", arg);
}
free(*line);
*line = tmp;
}
free(end);
}
static void write_file(char *name, int fd, char *line, t_arg *arg)
{
if (write(fd, line, ft_strlen(line)) == -1)
ft_exit(errno, name, arg);
if (write(fd, "\n", 1) == -1)
ft_exit(errno, name, arg);
}
void heredoc(char *name, char *limiter, t_arg *arg)
{
char *line;
int fd;
int i;
fd = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0644);
if (fd == -1)
ft_exit(errno, name, arg);
i = 1;
while (i)
{
write(1, "> ", 2);
gnl(&line, arg);
if (ft_strcmp_h(line, limiter))
write_file(name, fd, line, arg);
else
i = 0;
free(line);
}
close(fd);
}