-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipex.c
81 lines (75 loc) · 2.17 KB
/
pipex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rdanyell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/08 12:07:27 by rdanyell #+# #+# */
/* Updated: 2022/06/16 13:35:11 by rdanyell ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void child_proc(t_com *com, t_envp **env, t_exec *exec, int *pipefd)
{
if ((com->next || com->delim == 1) && com->delim != 4)
{
close(1);
dup2(pipefd[1], 1);
close(pipefd[1]);
close(pipefd[0]);
}
redirect_handler(com, pipefd);
executor(com, env, exec);
exit(g_exit_status);
}
void pipex(t_com *com, t_envp **env, t_exec *exec)
{
int pid;
int pipefd[2];
int exit_status;
if (com->delim != 0)
pipe(pipefd);
pid = fork();
if (pid == 0)
child_proc(com, env, exec, pipefd);
wait(&exit_status);
if (WIFSIGNALED(exit_status))
{
if (WTERMSIG(exit_status) != 13)
g_exit_status = 128 + WTERMSIG(exit_status);
if (WTERMSIG(exit_status) == SIGQUIT)
ft_putendl_fd("Quit", 2);
}
else
g_exit_status = WEXITSTATUS(exit_status);
close (0);
pipe_close(com, pipefd[0], pipefd[1]);
}
void pipe_handler(t_com *com, t_envp **env, t_exec *exec)
{
int save_stdout;
int i;
t_com *tmp;
save_stdout = dup(0);
tmp = com;
i = 0;
while (tmp && i <= exec->pipe_num)
{
pipex(tmp, env, exec);
if ((exec->pipe_num) > 0)
{
if (tmp->delim && tmp->delim != 1)
{
while (tmp->delim && tmp->delim != 1 && tmp->next)
tmp = tmp->next;
if (tmp->delim == 1 && !tmp->name)
tmp = tmp->next;
}
else
tmp = tmp->next;
}
i++;
}
std_out_close(save_stdout);
}