-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex.c
114 lines (104 loc) · 3 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/21 12:02:33 by brenaudo #+# #+# */
/* Updated: 2023/01/13 11:52:31 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void create_pipes(int *pipes);
static void close_pipe_and_recreate(int *pipes, t_cmd *cmd);
static void pipex_loop_core(int *children_pid, t_list *curr, int *pipes)
{
int built_in;
int ret;
built_in = builtin(((t_cmd *)curr->content)->str);
children_pid[((t_cmd *)curr->content)->cmd_idx] = fork();
if (children_pid[((t_cmd *)curr->content)->cmd_idx] == 0)
{
if (built_in == -1)
child(((t_cmd *)curr->content), pipes, children_pid);
else
call_builtin(built_in, ((t_cmd *)curr->content), pipes, \
children_pid);
}
if (built_in == -1)
change_sig_handling(((t_cmd *)curr->content)->str, pipes, children_pid);
else
{
waitpid(children_pid[((t_cmd *)curr->content)->cmd_idx], &ret, 0);
if (ret >> 8 == 0)
children_pid[((t_cmd *)curr->content)->cmd_idx] = -256;
else
children_pid[((t_cmd *)curr->content)->cmd_idx] = -1 * (ret >> 8);
}
g_glob->in_child = true;
close_pipe_and_recreate(pipes, ((t_cmd *)curr->content));
}
int *pipex_loop(void)
{
int pipes[4];
int *children_pid;
t_list *curr;
curr = *g_glob->cmds;
init_children_pid(&children_pid, ft_lstsize(curr) + 1);
create_pipes(pipes);
while (curr)
{
if (curr->content)
pipex_loop_core(children_pid, curr, pipes);
curr = curr->next;
}
close_pipes(pipes);
return (children_pid);
}
void pipex(void)
{
int i;
int exit_ret;
int *children_pid;
i = 0;
if (exit_parent() || cd_parent() || unset_parent() || export_parent())
return ;
children_pid = pipex_loop();
while (children_pid[i] != 0)
{
if (children_pid[i] > 0)
waitpid(children_pid[i], &exit_ret, 0);
pipex_handle_exit_ret(children_pid[i], exit_ret);
i++;
g_glob->sig_quit = false;
g_glob->sig_int = false;
}
g_glob->in_child = false;
init_sig_callbacks(0);
free(children_pid);
}
static void create_pipes(int *pipes)
{
pipe(pipes);
pipe(pipes + 2);
}
static void close_pipe_and_recreate(int *pipes, t_cmd *cmd)
{
if (cmd->cmd_idx % 2 == 0)
{
if (pipes[0] > 2)
close(pipes[0]);
if (pipes[1] > 2)
close(pipes[1]);
pipe(pipes);
}
else
{
if (pipes[2] > 2)
close(pipes[2]);
if (pipes[3] > 2)
close(pipes[3]);
pipe(pipes + 2);
}
}