Repository navigation
Expand file tree
/
Copy pathexec.c
More file actions
159 lines (145 loc) · 4.66 KB
/
Copy pathexec.c
File metadata and controls
159 lines (145 loc) · 4.66 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kmurugan <kmurugan@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/28 19:44:45 by kmurugan #+# #+# */
/* Updated: 2026/01/11 18:58:56 by kmurugan ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int exec_cmd(t_list *cmds, t_shell *shell);
static int is_builtin(char **s);
static int cmds_waitpid(t_list **pipe, t_shell *shell);
static void handle_sig_msg(int status);
/*
DESCRIPTION:
Executes the commands and returns the status.
It executes it in the parent process, if it is a single builtin command.
In case of multiple commands, it forks a child process for each command
and waits for them to finish.
It returns the status of the last command.
*/
int exec_cmds(t_list *cmds, t_shell *shell)
{
t_list *pipe;
t_cmd *cmd;
pipe = NULL;
while (cmds && cmds->content)
{
cmd = get_cmd(cmds);
if ((cmd->con == OR && !shell->status) || (cmd->con == AND
&& shell->status))
{
cmds = cmds->next;
while (cmds && cmds->content && get_cmd(cmds)->con == PIPE_CHAR)
cmds = cmds->next;
continue ;
}
shell->status = exec_cmd(cmds, shell);
if (!pipe)
pipe = cmds;
if (pipe && (!cmds->next || !cmds->next->content
|| get_cmd(cmds->next)->con != PIPE_CHAR)
&& cmds_waitpid(&pipe, shell) == -1)
exit_shell(cmds, shell);
cmds = cmds->next;
}
return (shell->status);
}
static int exec_cmd(t_list *cmds, t_shell *shell)
{
t_cmd *cmd;
cmd = get_cmd(cmds);
if (!cmd || (cmd->args && expand_args(cmd, shell))
|| (cmd->args && !*cmd->args))
return (0);
if (cmd->args && (!*cmd->args || !**cmd->args))
{
perr_msg("", E_CMD, NULL, false);
return (EXIT_CMD_NOT_FOUND);
}
cmd->exec.builtin = is_builtin(cmd->args);
if (cmd->exec.builtin == -1 || cmd->subshell || cmd->con == PIPE_CHAR
|| (cmds->next && (get_cmd(cmds->next)->con == PIPE_CHAR)))
{
if (fork_with_pipe(cmds, shell))
return (1);
return (shell->status);
}
return (exec_in_parent(cmds, shell));
}
/*
DESCRIPTION:
Checks if the command is a builtin command.
Returns the builtin command if it is, -1 otherwise.
*/
static int is_builtin(char **s)
{
if (!s || !*s)
return (-1);
if (!**s)
return (EMPTY_STRING);
if (ft_strcmp(*s, "echo") == 0)
return (BUILTIN_ECHO);
if (ft_strcmp(*s, "cd") == 0)
return (BUILTIN_CD);
if (ft_strcmp(*s, "pwd") == 0)
return (BUILTIN_PWD);
if (ft_strcmp(*s, "export") == 0)
return (BUILTIN_EXPORT);
if (ft_strcmp(*s, "unset") == 0)
return (BUILTIN_UNSET);
if (ft_strcmp(*s, "env") == 0)
return (BUILTIN_ENV);
if (ft_strcmp(*s, "exit") == 0)
return (BUILTIN_EXIT);
return (-1);
}
/*
DESCRIPTION:
Waits for the child processes to finish and returns the exit status of the
last command.
Returns the exit status of the last command on success, -1 on error.
waitpid(): Waits for a child process to update its status.
WIFEXITED() returns true if the child process terminated normally.
WIFSIGNALED() returns true if the child terminated due to a signal.
WEXITSTATUS() returns the exit status of the child process.
WTERMSIG() returns the signal that caused the child process to terminate.
If waitpid() returns -1, WIFSIGNALED or WEXITSTATUS is not set.
*/
static int cmds_waitpid(t_list **pipe, t_shell *shell)
{
int status;
if (set_signal_handler(SIGINT, SIG_IGN) == -1)
return (perror(MINI), -1);
while (*pipe && (*pipe)->content && get_cmd(*pipe)->exec.pid != -1)
{
if (waitpid(get_cmd(*pipe)->exec.pid, &status, 0) == -1)
{
perror(MINI);
shell->status = 1;
*pipe = (*pipe)->next;
continue ;
}
if (WIFEXITED(status))
shell->status = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
shell->status = SIG_EXIT_BASE + WTERMSIG(status);
*pipe = (*pipe)->next;
}
if (set_signal_handler(SIGINT, shell_handler) == -1)
return (perror(MINI), -1);
handle_sig_msg(shell->status);
*pipe = NULL;
return (shell->status);
}
static void handle_sig_msg(int status)
{
if (status == SIG_EXIT_BASE + SIGQUIT)
ft_dprintf(STDERR_FILENO, "%s\n", E_SIGQUIT);
else if (status == SIG_EXIT_BASE + SIGINT)
write(1, "\n", 1);
}