-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec2.c
111 lines (103 loc) · 3.09 KB
/
exec2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adel-cor <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/05 12:00:49 by adel-cor #+# #+# */
/* Updated: 2022/04/12 12:07:13 by adel-cor ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ms_builtins(char **arg, int i, t_job *job, t_cdata *c_data)
{
if (arg)
{
if (ft_strcmp(arg[0], "echo") == 0)
g_ex_status = built_echo(arg);
else if (ft_strcmp(arg[0], "cd") == 0)
g_ex_status = built_cd(arg[1], c_data);
else if (ft_strcmp(arg[0], "pwd") == 0)
g_ex_status = built_pwd();
else if (ft_strcmp(arg[0], "export") == 0)
g_ex_status = built_export(arg + 1, c_data);
else if (ft_strcmp(arg[0], "unset") == 0)
g_ex_status = built_unset(arg + 1, c_data);
else if (ft_strcmp(arg[0], "env") == 0)
g_ex_status = built_envp(c_data);
else if (ft_strcmp(arg[0], "exit") == 0)
built_exit(arg + 1);
else
return (1);
}
if (i == 0)
return (0);
free_job_lst(job);
exit(0);
}
int ms_exec_builtins(t_job *job, t_cdata *c_data)
{
int saved_stdin;
int saved_stdout;
saved_stdin = dup(0);
saved_stdout = dup(1);
if (job && job->next == NULL)
{
if (job->cmd && check_builtins(job->cmd) == 1)
return (0);
if (check_redirection(job, 1) == 1)
return (1);
if (ms_builtins(job->cmd, 0, job, c_data) == 0)
{
restore_fd(saved_stdin, saved_stdout);
return (1);
}
}
return (0);
}
void init_pipe(t_job *job)
{
while (job)
{
pipe(job->fd);
job = job->next;
}
}
void child_process(t_job *job, t_job *first, t_cdata *c_data)
{
job->pid = fork();
if (job->pid == -1)
ft_putendl_fdnl("Dang! This fork didn't work!", 2);
if (job->pid == 0)
{
ft_shortcut_events_interactive();
if (job->previous != NULL)
dup2(job->previous->fd[0], STDIN_FILENO);
if (job->next != NULL)
dup2(job->fd[1], STDOUT_FILENO);
check_redirection(job, 0);
close(job->fd[0]);
close(job->fd[1]);
free_fd(first);
if (job->cmd && ms_builtins(job->cmd, 1, first, c_data) == 1)
execute(job->cmd, first, c_data);
}
else
ft_ignore_all_signals();
if (job->previous != NULL)
close(job->previous->fd[0]);
close(job->fd[1]);
}
void executor(t_job *job, t_cdata *c_data)
{
t_job *first;
first = job;
init_pipe(first);
if (make_heredocs(job, c_data) == 1 || ms_exec_builtins(job, c_data) == 1)
return ;
if (job && job->cmd)
{
mini_exec(job, first, c_data);
}
}