-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.c
92 lines (85 loc) · 2.23 KB
/
pipeline.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
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipeline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yufukuya <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/05 17:36:33 by yufukuya #+# #+# */
/* Updated: 2021/02/05 21:26:16 by yufukuya ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "minishell.h"
void setup_pipe(int ispipe, int haspipe, int newpipe[2], int lastpipe[2])
{
if (haspipe)
{
close(lastpipe[1]);
if (dup2(lastpipe[0], 0) == -1)
die(strerror(errno));
close(lastpipe[0]);
}
if (ispipe)
{
close(newpipe[0]);
if (dup2(newpipe[1], 1) == -1)
die(strerror(errno));
close(newpipe[1]);
}
}
void cleanup_pipe(int haspipe, int lastpipe[2])
{
if (haspipe)
{
close(lastpipe[0]);
close(lastpipe[1]);
}
}
t_command *do_pipeline(t_command *c)
{
int ispipe;
int haspipe;
int lastpipe[2];
ispipe = 0;
haspipe = 0;
lastpipe[0] = -1;
lastpipe[1] = -1;
while (c)
{
ispipe = c->op == OP_PIPE ? 1 : 0;
c->pid = start_command(c->argv, ispipe, haspipe, lastpipe);
haspipe = ispipe;
if (ispipe && c->next)
c = c->next;
else if (ispipe && !c->next)
die("failed to do pipelin");
else
break ;
}
return (c);
}
void wait_pipeine(pid_t pid)
{
pid_t exited_pid;
int status;
exited_pid = waitpid(pid, &status, 0);
if (exited_pid != pid)
die("failed to wait");
if (WIFEXITED(status))
{
g_exit_status = WEXITSTATUS(status);
}
else if (WIFSIGNALED(status))
{
g_exit_status = 128 + WTERMSIG(status);
if (WTERMSIG(status) == SIGQUIT)
ft_putstr_fd("Quit: 3\n", 2);
else
ft_putstr_fd("\n", 2);
}
else
die("child exited abnormally");
while (wait(NULL) > 0)
;
}