-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
144 lines (134 loc) · 4.35 KB
/
executor.c
File metadata and controls
144 lines (134 loc) · 4.35 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* executor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maustel <maustel@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 11:59:17 by maustel #+# #+# */
/* Updated: 2024/12/01 13:07:09 by maustel ### ########.fr */
/* */
/* ************************************************************************** */
#include "../minishell.h"
/*-------------------------------------------------------------
Handle pipechain
---------------------------------------------------------------*/
int execute_pipechain(int nbr_pipes, t_shell *shell)
{
int n;
if (init_fd_pid(shell, nbr_pipes))
return (1);
n = 0;
while (n < nbr_pipes)
{
if (pipe(shell->fd[n]) == -1)
return (print_error(errno, NULL, PRINT));
n++;
}
if (pipechain_loop(shell))
return (1);
if (pipe_parent(shell, nbr_pipes))
return (2);
return (0);
}
/*-------------------------------------------------------------
Handle parent for single command
waitpid() blocks until the child process terminates or until a signal occurs
WIFEXITED(wstatus) macro checks if the process exited normally
WEXITwSTATUS(wstatus) extracts the exit wstatus value from the wstatus argument
if (WIFSIGNALED(wstatus)): if interrupted with signal
---------------------------------------------------------------*/
int single_parent(pid_t id, char *cmd)
{
int wstatus;
int exit_code;
exit_code = 0;
if (waitpid(id, &wstatus, 0) == -1)
return (1);
if (WIFEXITED(wstatus))
exit_code = WEXITSTATUS(wstatus);
if (WIFSIGNALED(wstatus))
exit_code = WTERMSIG(wstatus) + 128;
if (exit_code > 0)
return (print_error(exit_code, cmd, NOTPRINT));
return (exit_code);
}
/*-------------------------------------------------------------
Handle child for single command
if execve fails, its because of command not found (127)
child then exits with 127 and the parent will store the exit state
if it doesnt fail, exit code is 0
---------------------------------------------------------------*/
void single_child(char *path, t_command *row, t_shell *shell)
{
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
if (execve(path, row->args, shell->env))
{
print_error(127, row->args[0], PRINT);
free_child_exit(shell, 127);
}
}
/*-------------------------------------------------------------
execute single command without pipe
---------------------------------------------------------------*/
int execute_single_command(t_command *row, t_shell *shell)
{
pid_t id;
if (check_files(row))
return (1);
if (exec_redirections(row))
return (2);
if (check_builtins(shell, row) < 1)
return (0);
if (get_check_path(row, shell->env))
return (3);
if (row->args[0])
{
id = fork();
if (id == -1)
return (print_error(errno, NULL, PRINT));
else if (id == 0)
single_child(row->path, row, shell);
else if (id > 0)
{
if (single_parent(id, row->args[0]))
return (2);
}
}
reset_redirections(row);
return (0);
}
/*-------------------------------------------------------------
Main function for executor
signal() ignores the handle_signals().
This is needed to catch the exit code of such programs like cat and sleep.
and also for ./minishell in minishell
---------------------------------------------------------------*/
int executor(t_shell *shell)
{
t_command *current_cmd;
int nbr_pipes;
shell->pid = NULL;
shell->fd = NULL;
if (shell->syntax_error == true)
return (1);
if (handle_heredoc(shell->table, shell->env))
return (2);
signal(SIGINT, SIG_IGN);
nbr_pipes = ft_lstsize(shell->table) - 1;
if (nbr_pipes == 0)
{
current_cmd = (t_command *) shell->table->content;
set_original_std(current_cmd);
if (execute_single_command(current_cmd, shell))
return (reset_redirections(current_cmd), 3);
}
else if (nbr_pipes > 0)
{
if (execute_pipechain(nbr_pipes, shell))
return (free_fd_pid(shell, nbr_pipes), 4);
free_fd_pid(shell, nbr_pipes);
}
return (0);
}