mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
exec: retain exit status of commands, including if they were signaled
This commit is contained in:
parent
411ceaf367
commit
1b41f49341
2 changed files with 30 additions and 3 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
||||||
/* Updated: 2025/04/02 18:19:57 by khais ### ########.fr */
|
/* Updated: 2025/04/02 18:45:47 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -18,13 +18,25 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
static void command_not_found(t_simple_cmd *cmd)
|
static void command_not_found(t_simple_cmd *cmd)
|
||||||
{
|
{
|
||||||
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
|
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
|
||||||
cmd->words->word->word);
|
cmd->words->word->word);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void do_waitpid(t_minishell *app, int pid)
|
||||||
|
{
|
||||||
|
int wstatus;
|
||||||
|
|
||||||
|
waitpid(pid, &wstatus, 0);
|
||||||
|
if (WIFEXITED(wstatus))
|
||||||
|
app->last_return_value = WEXITSTATUS(wstatus);
|
||||||
|
if (WIFSIGNALED(wstatus))
|
||||||
|
app->last_return_value = 128 + WTERMSIG(wstatus);
|
||||||
|
}
|
||||||
|
|
||||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
||||||
{
|
{
|
||||||
char *exe;
|
char *exe;
|
||||||
|
|
@ -44,6 +56,6 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
execute_subprocess(exe, cmd, app);
|
execute_subprocess(exe, cmd, app);
|
||||||
free(exe);
|
free(exe);
|
||||||
waitpid(pid, NULL, 0);
|
do_waitpid(app, pid);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
test.sh
15
test.sh
|
|
@ -342,6 +342,21 @@ expecting <<EOF
|
||||||
minishell: : command not found
|
minishell: : command not found
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
when_run <<EOF "exit status of last command is preserved"
|
||||||
|
echo \$?
|
||||||
|
ls nonexist
|
||||||
|
echo \$?
|
||||||
|
echo hi
|
||||||
|
echo \$?
|
||||||
|
EOF
|
||||||
|
expecting <<EOF
|
||||||
|
0
|
||||||
|
ls: cannot access 'nonexist': No such file or directory
|
||||||
|
2
|
||||||
|
hi
|
||||||
|
0
|
||||||
|
EOF
|
||||||
|
|
||||||
when_run <<EOF "quoted parentheses are not operators"
|
when_run <<EOF "quoted parentheses are not operators"
|
||||||
echo unclosed '('
|
echo unclosed '('
|
||||||
EOF
|
EOF
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue