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
5b7367925f
commit
24ba87abba
2 changed files with 29 additions and 2 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
||||
/* Updated: 2025/04/02 18:19:57 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/15 15:01:17 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -18,6 +18,7 @@
|
|||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
char *exe;
|
||||
|
|
@ -44,6 +56,6 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
|||
if (pid == 0)
|
||||
execute_subprocess(exe, cmd, app);
|
||||
free(exe);
|
||||
waitpid(pid, NULL, 0);
|
||||
do_waitpid(app, pid);
|
||||
return ;
|
||||
}
|
||||
|
|
|
|||
15
test.sh
15
test.sh
|
|
@ -342,6 +342,21 @@ expecting <<EOF
|
|||
minishell: : command not found
|
||||
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"
|
||||
echo unclosed '('
|
||||
EOF
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue