forked from unow0517/42_minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.c
More file actions
108 lines (99 loc) · 3.38 KB
/
execution.c
File metadata and controls
108 lines (99 loc) · 3.38 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
98
99
100
101
102
103
104
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsimitop <tsimitop@student.42heilbronn. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 18:12:00 by tsimitop #+# #+# */
/* Updated: 2024/05/25 18:12:00 by tsimitop ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void execution_cases(t_shell *shell_info)
{
pid_t pid;
pid = 0;
if (shell_info->syntax_error == false)
{
if (num_of_total_cmds(shell_info->first_command) == 1 && \
shell_info->first_command->is_builtin == true)
execute_builtin(shell_info, shell_info->first_command);
else
{
pid = exec_pipeline(shell_info);
waitpid(pid, shell_info->status, 0);
while (waitpid(-1, NULL, WNOHANG) != -1)
;
*shell_info->status = handle_exit(*shell_info->status);
}
}
}
void execute_builtin(t_shell *shell_info, t_command *cmd)
{
if (inputis(cmd->builtin_type, "echo"))
run_echo(cmd->builtin_arg, shell_info, cmd->output_fd);
else if (inputis(cmd->builtin_type, "cd"))
run_cd(cmd->builtin_arg, shell_info);
else if (inputis(cmd->builtin_type, "pwd"))
run_pwd(shell_info);
else if (inputis(cmd->builtin_type, "env"))
run_env(shell_info);
else if (inputis(cmd->builtin_type, "export"))
run_export(cmd->builtin_arg, shell_info);
else if (inputis(cmd->builtin_type, "unset"))
run_unset(cmd->builtin_arg, shell_info);
else if (inputis(cmd->builtin_type, "history"))
print_history(shell_info);
}
pid_t exec_pipeline(t_shell *shell_info)
{
t_command *iterate_cmd;
pid_t pid;
pid = 0;
iterate_cmd = shell_info->first_command;
while (iterate_cmd)
{
pid = exec_single_cmd(shell_info, iterate_cmd);
iterate_cmd = iterate_cmd->next;
}
close_pipes(shell_info);
return (pid);
}
pid_t exec_single_cmd(t_shell *shell_info, t_command *cmd_to_exec)
{
pid_t pid;
char *full_path;
char **paths_in_env;
pid = fork();
if (pid == -1)
fork_fail();
if (cmd_to_exec->is_builtin == false)
{
paths_in_env = ft_path_in_envmini(shell_info->env_mini);
full_path = find_cmd_in_env_mini(cmd_to_exec->cmd, paths_in_env);
}
if (pid == 0)
{
child_proccess(shell_info, cmd_to_exec, full_path, paths_in_env);
exit(EXIT_FAILURE);
}
else
{
if (cmd_to_exec->is_builtin == false)
free_exec_paths(full_path, paths_in_env);
close_fds(shell_info, cmd_to_exec);
return (pid);
}
}
void child_proccess(t_shell *shell_info, t_command *cmd_to_exec, \
char *full_path, char **paths_in_env)
{
pipe_handling(shell_info, cmd_to_exec);
handle_redir(shell_info, cmd_to_exec);
if (cmd_to_exec->file_not_found == 0)
execute_cmd(shell_info, cmd_to_exec, full_path, paths_in_env);
close_fds(shell_info, cmd_to_exec);
if (cmd_to_exec->is_builtin == false)
free_exec_paths(full_path, paths_in_env);
}