From dd12a170d96cee39f12989175506e469f3a1f005 Mon Sep 17 00:00:00 2001 From: whaffman Date: Thu, 6 Mar 2025 11:38:16 +0100 Subject: [PATCH] report last exit status --- inc/executor.h | 4 ++-- src/executor/executor_child.c | 19 +++++++++++-------- src/executor/executor_fork.c | 7 ++++--- src/main.c | 18 ++++++++++-------- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/inc/executor.h b/inc/executor.h index 6799399..659de3e 100644 --- a/inc/executor.h +++ b/inc/executor.h @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 17:06:07 by willem #+# #+# */ -/* Updated: 2025/02/26 15:45:17 by whaffman ######## odam.nl */ +/* Updated: 2025/03/06 11:35:39 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ # include "minishell.h" char *executor_absolute_path(t_minishell *msh, char *cmd); -void executor_child(t_minishell *msh, t_command *command); +int executor_child(t_minishell *msh, t_command *command); pid_t executor_fork(t_minishell *msh, t_command *command); void executor_create_pipes(t_minishell *msh); int executor_execute_pipeline(t_minishell *msh); diff --git a/src/executor/executor_child.c b/src/executor/executor_child.c index 6b83d58..79fab4d 100644 --- a/src/executor/executor_child.c +++ b/src/executor/executor_child.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:10 by willem #+# #+# */ -/* Updated: 2025/03/02 22:28:55 by whaffman ######## odam.nl */ +/* Updated: 2025/03/06 11:36:33 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,19 +29,20 @@ static int validate_executable_path(char *path) if (access(path, X_OK) < 0) { error_msg(path, "Permission denied"); - return (0); + return (127); } if (is_dir(path)) { error_msg(path, "Is a directory"); - return (0); + return (126); } - return (1); + return (0); } -void executor_child(t_minishell *msh, t_command *command) +int executor_child(t_minishell *msh, t_command *command) { char *path; + int exit_status; if (command->fd_in != 0) dup2(command->fd_in, 0); @@ -53,9 +54,11 @@ void executor_child(t_minishell *msh, t_command *command) { errno = 0; error_msg("minishell", "command not found"); - return ; + return (127); } - else if (! validate_executable_path(path)) - return ; + exit_status = validate_executable_path(path); + if (exit_status != 0) + return (exit_status) ; execve(path, command->args, environment_get_arr(msh)); + return (126); } diff --git a/src/executor/executor_fork.c b/src/executor/executor_fork.c index 4b359e6..8a604d2 100644 --- a/src/executor/executor_fork.c +++ b/src/executor/executor_fork.c @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:24:52 by willem #+# #+# */ -/* Updated: 2025/02/26 17:44:36 by whaffman ######## odam.nl */ +/* Updated: 2025/03/06 11:34:43 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ pid_t executor_fork(t_minishell *msh, t_command *command) { pid_t pid; + int exit_status; pid = fork(); if (pid > 0) @@ -29,8 +30,8 @@ pid_t executor_fork(t_minishell *msh, t_command *command) else if (pid == 0) { signal_init_child(); - executor_child(msh, command); - exit(127); + exit_status = executor_child(msh, command); + exit(exit_status); } else error_msg("minishell", "fork failed"); diff --git a/src/main.c b/src/main.c index 3a54fa0..e0220a6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ -/* Updated: 2025/03/05 15:17:58 by qmennen ### ########.fr */ +/* :::::::: */ +/* main.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ +/* Updated: 2025/03/06 11:24:46 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,6 +53,7 @@ static void main_test(t_minishell *msh, char *line) int main(int argc, char **argv, char **envp) { t_minishell *msh; + int exit_status; print_banner(); history_load(); @@ -63,7 +64,8 @@ int main(int argc, char **argv, char **envp) main_test(msh, argv[2]); else main_loop(msh); + exit_status = msh->exit_status; free_minishell(&msh); rl_clear_history(); - return (EXIT_SUCCESS); + return (exit_status); }