-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.c
101 lines (93 loc) · 3.04 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rdanyell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 16:18:54 by rdanyell #+# #+# */
/* Updated: 2022/06/16 16:49:53 by rdanyell ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void make_full_com(t_com *com, t_exec *exec)
{
t_com *full_com;
t_com *com_name;
int com_num;
com_num = 1;
full_com = com;
com_name = com;
while (full_com && full_com->delim != 1 && com_num++)
full_com = full_com->next;
exec->cmd_full = (char **)malloc(sizeof(char *) * (com_num * 10));
if (!exec->cmd_full)
return ;
if (com_name->delim == 0 || com_name->delim == 1)
check_cmd(com_name, exec);
while (com_name && com_name->delim != 1)
{
if (com_name->name)
{
check_cmd(com_name, exec);
break ;
}
com_name = com_name->next;
}
find_args(com, exec);
}
void execve_handler(t_com *com, t_exec *exec, t_envp *envp_list)
{
int exit_status;
int pid;
pid = fork();
if (pid == 0)
{
get_env(envp_list, exec);
make_full_com(com, exec);
execve(exec->cmd_exec, exec->cmd_full, exec->env);
ft_putstr_fd("Myshell 🐚: ", 2);
perror(exec->cmd_full[0]);
g_exit_status = 127;
free_pipe_struct(exec);
exit(127);
}
wait(&exit_status);
if (WIFSIGNALED(exit_status))
{
if (WTERMSIG(exit_status) != 13)
g_exit_status = 128 + WTERMSIG(exit_status);
if (WTERMSIG(exit_status) == SIGQUIT)
ft_putendl_fd("Quit", 2);
}
else
g_exit_status = WEXITSTATUS(exit_status);
}
void executor(t_com *com, t_envp **envp_list, t_exec *exec)
{
if (!ft_strcmp(com->name, "echo") || !ft_strcmp(com->name, "ECHO"))
g_exit_status = builtin_echo(com);
else if (!ft_strcmp(com->name, "env") || !ft_strcmp(com->name, "ENV"))
g_exit_status = builtin_env(*envp_list);
else if (!ft_strcmp(com->name, "pwd") || !ft_strcmp(com->name, "PWD"))
g_exit_status = builtin_pwd(*envp_list);
else if (!ft_strcmp(com->name, "cd"))
g_exit_status = builtin_cd(com, *envp_list);
else if (!ft_strcmp(com->name, "export"))
g_exit_status = builtin_export(com, envp_list);
else if (!ft_strcmp(com->name, "unset"))
g_exit_status = builtin_unset(com, envp_list);
else if (!ft_strcmp(com->name, "exit"))
builtin_exit(com, *envp_list);
else
execve_handler(com, exec, *envp_list);
}
void execute(t_com *com, t_envp **env)
{
t_exec exec;
exec.pipe_num = count_pipes(com);
if (com->delim != 0)
pipe_handler(com, env, &exec);
else
executor(com, env, &exec);
}