-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
107 lines (102 loc) · 2.39 KB
/
exec.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
#include "../minishell.h"
void handle_command(t_lst *commands)
{
commands->save_stdin = dup(STDIN_FILENO);
commands->save_stdout = dup(STDOUT_FILENO);
if (open_files(commands) == -1 && g_data.nb_of_commands == 1)
return ;
if (g_data.here_doc != 1)
{
if (!ft_strcmp(commands->content[0], "top"))
signal(SIGINT, sighandler_cmd1);
else
signal(SIGINT, sighandler_cmd);
signal(SIGQUIT, sighandler_cmd);
}
if (g_data.nb_of_commands == 1)
{
if (handle_one_command(commands) == -1)
return ;
}
else
{
if (pipex(commands, STDIN_FILENO) == -1)
return ;
}
close_files(commands);
}
int execute_one_command(t_lst *commands)
{
commands->pid = fork();
if (commands->pid < 0)
return (-1);
else if (commands->pid == 0)
{
if (redirect_files(commands) == -1)
exit(1);
exec_cmd(commands, 0);
}
else
{
if (waitpid(-1, &commands->status, 0) == -1)
return (-1);
if (WIFEXITED(commands->status))
g_data.exit_code = WEXITSTATUS(commands->status);
if (redirect_standard(commands) == -1)
return (-1);
}
return (0);
}
int handle_one_command(t_lst *commands)
{
if (!commands->cmd[0])
{
printf("bash: syntax error near unexpected token\n");
return (-1);
}
test_built(commands);
if (g_data.built == 1)
{
if (redirect_files(commands) == -1)
return (-1);
execute_builtin(commands);
redirect_standard(commands);
}
else
{
if (execute_one_command(commands) == -1)
return (-1);
}
return (0);
}
void test_built(t_lst *commands)
{
g_data.built = 0;
if (!ft_strcmp(commands->cmd[0], "cd")
|| !ft_strcmp(commands->cmd[0], "echo")
|| !ft_strcmp(commands->cmd[0], "env")
|| !ft_strcmp(commands->cmd[0], "exit")
|| !ft_strcmp(commands->cmd[0], "export")
|| !ft_strcmp(commands->cmd[0], "pwd")
|| !ft_strcmp(commands->cmd[0], "unset"))
{
g_data.built = 1;
}
}
void execute_builtin(t_lst *commands)
{
if (!ft_strcmp(commands->cmd[0], "cd"))
g_data.exit_code = cd(commands);
else if (!ft_strcmp(commands->cmd[0], "echo"))
g_data.exit_code = echo(commands);
else if (!ft_strcmp(commands->cmd[0], "env"))
g_data.exit_code = env(commands);
else if (!ft_strcmp(commands->cmd[0], "exit"))
ft_exit(commands);
else if (!ft_strcmp(commands->cmd[0], "export"))
g_data.exit_code = export(commands);
else if (!ft_strcmp(commands->cmd[0], "pwd"))
g_data.exit_code = pwd(commands);
else if (!ft_strcmp(commands->cmd[0], "unset"))
g_data.exit_code = unset(commands);
}