-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecutor.c
More file actions
59 lines (55 loc) · 2.21 KB
/
Copy pathexecutor.c
File metadata and controls
59 lines (55 loc) · 2.21 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* executor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asabani <asabani@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/01 16:12:41 by asabani #+# #+# */
/* Updated: 2022/03/03 00:45:43 by asabani ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*
* Save stdin/stdout around a redirection chain so that any redirects
* applied to a builtin do not leak into commands that follow on the
* same line (e.g. `echo a > /tmp/x && echo b`).
* For commands that run in a child process (external cmds, subshells,
* pipelines) the restore happens in the parent, so it is harmless.
*/
static void exec_redirection(t_cmdtree *tree)
{
int saved[2];
saved[0] = dup(STDIN_FILENO);
saved[1] = dup(STDOUT_FILENO);
run_redirection((t_redir *)tree, 1);
if (saved[0] != -1)
{
dup2(saved[0], STDIN_FILENO);
close(saved[0]);
}
if (saved[1] != -1)
{
dup2(saved[1], STDOUT_FILENO);
close(saved[1]);
}
}
void executor(t_cmdtree *tree)
{
if (!tree)
return ;
if (tree->node_type == NODE_CMDLST)
return (run_cmdlist((t_cmdlist *)tree));
else if (tree->node_type == NODE_PIPE)
return (run_pipeline((t_connector *)tree));
else if (tree->node_type == NODE_AND || tree->node_type == NODE_OR)
return (run_logical_connector((t_connector *)tree, tree->node_type));
else if (tree->node_type == NODE_SUBSH)
return (run_subshell((t_subsh *)tree));
else if (tree->node_type == NODE_FG)
return (run_fg_connector((t_connector *)tree));
else if (tree->node_type == NODE_BG)
return (run_bg_connector((t_connector *)tree));
else if (tree->node_type == NODE_REDIR)
exec_redirection(tree);
}