-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_utils2.c
More file actions
118 lines (109 loc) · 3.24 KB
/
Copy pathexecutor_utils2.c
File metadata and controls
118 lines (109 loc) · 3.24 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
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
115
116
117
118
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* executor_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yucchen <yucchen@student.42singapore.sg +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 16:47:34 by yucchen #+# #+# */
/* Updated: 2026/01/09 14:52:15 by yucchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
// redir-only commands still should create/truncate files
void exec_child_cmd(t_cmd *cmd, t_env **env, char **envp, t_cmd *cmd_head)
{
int code;
t_child child;
child.cmd_head = cmd_head;
child.env = env;
child.envp = envp;
if (apply_redirs(cmd))
{
child_cleanup(child.cmd_head, child.env, child.envp);
exit(EXIT_FAILURE);
}
if (!cmd->argv || !cmd->argv[0])
{
child_cleanup(child.cmd_head, child.env, child.envp);
exit(EXIT_SUCCESS);
}
if (is_builtin(cmd->argv[0]))
{
code = run_builtin(cmd, env);
child_cleanup(child.cmd_head, child.env, child.envp);
exit(code);
}
try_exec(cmd, &child);
}
static void restore_error(int dup_stdin, int dup_stdout)
{
if (dup2(dup_stdin, STDIN_FILENO) == -1)
perror("dup2 restore stdin");
if (dup2(dup_stdout, STDOUT_FILENO) == -1)
perror("dup2 restore stdout");
}
int run_exit_in_parent(t_cmd *cmd)
{
int dup_stdin;
int dup_stdout;
int need_exit;
need_exit = 0;
ft_putendl_fd("exit", 2);
if (!cmd->redirs)
return (should_exit(cmd->argv));
dup_stdin = dup(STDIN_FILENO);
dup_stdout = dup(STDOUT_FILENO);
if (dup_stdin == -1 || dup_stdout == -1)
{
if (dup_stdin != -1)
close(dup_stdin);
if (dup_stdout != -1)
close(dup_stdout);
return (perror("dup"), g_last_status = 1, 0);
}
if (!apply_redirs(cmd))
need_exit = should_exit(cmd->argv);
else
g_last_status = 1;
restore_error(dup_stdin, dup_stdout);
return (close(dup_stdin), close(dup_stdout), need_exit);
}
int run_builtin_in_parent(t_cmd *cmd, t_env **env)
{
int dup_stdin;
int dup_stdout;
if (!cmd->redirs)
return (g_last_status = run_builtin(cmd, env), 0);
dup_stdin = dup(STDIN_FILENO);
dup_stdout = dup(STDOUT_FILENO);
if (dup_stdin == -1 || dup_stdout == -1)
{
if (dup_stdin != -1)
close(dup_stdin);
if (dup_stdout != -1)
close(dup_stdout);
return (perror("dup"), g_last_status = 1, 0);
}
if (!apply_redirs(cmd))
g_last_status = run_builtin(cmd, env);
else
g_last_status = 1;
restore_error(dup_stdin, dup_stdout);
return (close(dup_stdin), close(dup_stdout), 0);
}
void exitcode_from_status(int status)
{
if (WIFEXITED(status))
g_last_status = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
{
if (WTERMSIG(status) == SIGINT)
write(1, "\n", 1);
else if (WTERMSIG(status) == SIGQUIT)
write(1, "Quit (core dumped)\n", 19);
g_last_status = 128 + WTERMSIG(status);
}
else
g_last_status = 1;
}