-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.c
More file actions
97 lines (86 loc) · 2.64 KB
/
exec.c
File metadata and controls
97 lines (86 loc) · 2.64 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edamar <edamar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/20 22:13:34 by adurusoy #+# #+# */
/* Updated: 2023/12/28 11:46:33 by edamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <stdlib.h>
#include <unistd.h>
void run_command(char **env, t_parse *tmp, int *fd, t_shell *m_shell)
{
int control;
control = is_builtin(tmp);
if (control)
execute_builtin_command(tmp, m_shell);
else
run_execve(tmp, env, fd, m_shell);
free_env(m_shell);
free_loop(1, m_shell);
if (m_shell->lex_list)
free(m_shell->lex_list);
free(m_shell);
exit(0);
}
void run_single_command(char **env, t_parse *parse, t_shell *m_shell)
{
int control;
control = 0;
if (parse->type != HEREDOC)
control = is_builtin(parse);
if (control)
execute_builtin_command(m_shell->parse, m_shell);
else
run_execve(m_shell->parse, env, NULL, m_shell);
}
void multi_command(t_parse *parse, char **env, t_shell *m_shell, int *fd)
{
t_parse *nparse;
while (parse)
{
if (parse->next)
pipe(fd);
nparse = get_next_cmd(&parse);
parse->pid = fork();
if (parse->pid == 0)
{
create_dup_one(parse, fd);
run_command(env, parse, fd, m_shell);
}
if (nparse)
create_dup_two(nparse, fd);
parse = nparse;
}
}
void run_multi_command(char **env, t_parse *parse, t_shell *m_shell)
{
int fd[2];
m_shell->parse->std_in = dup(0);
multi_command(parse, env, m_shell, fd);
dup2(m_shell->parse->std_in, 0);
clear_pipe(fd);
wait_all(m_shell);
}
void exec(char **env, t_shell *m_shell)
{
int x;
if (g_check_heredoc != 0)
loop_heredoc(m_shell);
if (m_shell->parse->cmd && !ft_strcmp(m_shell->parse->cmd, "exit")
&& g_check_heredoc != -10 && get_next_cmd(&m_shell->parse) == NULL)
{
builtin_exit(&m_shell);
return ;
}
x = single_or_multi_command(m_shell);
if (!x && g_check_heredoc != -10)
run_single_command(env, m_shell->parse, m_shell);
else if (g_check_heredoc != -10)
run_multi_command(env, m_shell->parse, m_shell);
g_check_heredoc = 0;
}