-
Notifications
You must be signed in to change notification settings - Fork 0
/
heredoc.c
77 lines (71 loc) · 1.8 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bde-sous <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/30 14:26:48 by bde-sous #+# #+# */
/* Updated: 2023/10/12 21:53:51 by bde-sous ### ########.fr */
/* */
/* ************************************************************************** */
#include "redirect.h"
int ft_heredoc_aux(char *str, char *escape, int fd[2])
{
str = readline("> ");
if (str == NULL)
{
free(str);
return (0);
}
if (ft_strcmp(str, escape) == 0)
{
free(str);
return (0);
}
ft_putstr_fd(str, fd[1]);
ft_putstr_fd("\n", fd[1]);
free(str);
return (1);
}
int ft_heredoc_aux2(char *str, char *escape, int fd[2], int pid)
{
int fd_return;
if (pid == 0)
{
close(fd[0]);
signal(SIGINT, handle_heredoc);
while (ft_heredoc_aux(str, escape, fd))
;
close(fd[1]);
exit(0);
}
else
{
signal(SIGINT, SIG_IGN);
close(fd[1]);
waitpid(pid, NULL, 0);
fd_return = dup(fd[0]);
close(fd[0]);
}
return (fd_return);
}
int ft_heredoc(char *escape)
{
int fd[2];
int pid;
char *str;
str = NULL;
if (pipe(fd) == -1)
{
perror("pipe");
return (-1);
}
pid = fork();
if (pid == -1)
{
perror("fork");
return (-1);
}
return (ft_heredoc_aux2(str, escape, fd, pid));
}