exec: retain exit status of commands, including if they were signaled

This commit is contained in:
Khaïs COLIN 2025-04-02 15:26:11 +02:00
parent 3661feefaa
commit f4e9955f75
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 30 additions and 3 deletions

View file

@ -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,6 +18,7 @@
#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)
{ {
@ -25,6 +26,17 @@ static void command_not_found(t_simple_cmd *cmd)
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
View file

@ -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