-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.c
130 lines (121 loc) · 3.43 KB
/
execute.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abuzdin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/20 09:30:25 by abuzdin #+# #+# */
/* Updated: 2022/08/11 18:35:01 by abuzdin ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// check for builtin and execute it if it exists
int check_builtin(t_input *data, t_cmd *cmds)
{
int i;
i = 0;
while (i < 7)
{
if (ft_strcmp(cmds->cmd[0], data->builtins[i].name))
++i;
else
{
data->builtins[i].func(data);
return (1);
}
}
return (0);
}
// fork for all cmds except the last one; dup2 input and output files
void ms_fork(char *argv[], t_input *data)
{
int fd[2];
error_check(pipe(fd), "", data);
data->cmds->pid = fork();
error_check(data->cmds->pid, "", data);
if (data->cmds->pid == 0)
{
if (data->cmds->in_arg)
error_check(dup2(data->cmds->in, IN),
data->cmds->in_arg, data);
if (data->cmds->out_arg && !is_builtin(data, data->cmds))
error_check(dup2(data->cmds->out, OUT),
data->cmds->out_arg, data);
else
error_check(dup2(fd[1], OUT), "", data);
close_fds(fd[1], fd[0], 0);
if (check_builtin(data, data->cmds))
exit(g_status);
else
ms_execve(argv, data);
}
close(data->cmds->in);
error_check(dup2(fd[0], IN), "", data);
close_fds(fd[1], fd[0], 0);
}
static void last_cmd(t_input *data)
{
error_check(dup2(data->cmds->in, IN), "", data);
error_check(dup2(data->cmds->out, OUT), "", data);
if (check_builtin(data, data->cmds))
exit(g_status);
else
ms_execve(data->cmds->cmd, data);
close_fds(data->cmds->in, data->cmds->out, 1);
}
// loop until the last command then execve for the last one
int pipex(t_input *data)
{
t_cmd *head;
head = data->cmds;
while (data->cmds->pipe == 1)
{
ms_fork(data->cmds->cmd, data);
data->cmds = data->cmds->next;
}
data->cmds->pid = fork();
if (data->cmds->pid == 0)
last_cmd(data);
while (head)
{
if (head->next)
{
close(0);
close(1);
}
waitpid(head->pid, &g_status, 0);
if (WIFSIGNALED(data->cmds->pid) && g_status)
g_status += 128;
head = head->next;
}
exit(g_status);
}
// if there is no pipe or no builtin, execute simple command
// otherwise do pipex or execute builtin (without fork)
int execute(t_input *data)
{
if (signal(SIGINT, signal_fork) == SIG_ERR
|| signal(SIGQUIT, signal_fork) == SIG_ERR)
error_check(-1, "", data);
if (data->cmds->pipe == 1 || !check_builtin(data, data->cmds))
{
data->pid = fork();
error_check(data->pid, "", data);
if (data->pid == 0)
{
if (data->cmds->pipe == 1)
pipex(data);
else
{
set_std(data, data->cmds->in, data->cmds->out);
ms_execve(data->cmds->cmd, data);
close_fds(data->cmds->in, data->cmds->out, 1);
}
}
waitpid(data->pid, &g_status, 0);
if (WIFSIGNALED(data->pid) && g_status)
g_status += 128;
}
return (g_status);
}