report last exit status

This commit is contained in:
whaffman 2025-03-06 11:38:16 +01:00
parent 98ede770a5
commit dd12a170d9
4 changed files with 27 additions and 21 deletions

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/08 17:06:07 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" # include "minishell.h"
char *executor_absolute_path(t_minishell *msh, char *cmd); 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); pid_t executor_fork(t_minishell *msh, t_command *command);
void executor_create_pipes(t_minishell *msh); void executor_create_pipes(t_minishell *msh);
int executor_execute_pipeline(t_minishell *msh); int executor_execute_pipeline(t_minishell *msh);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/12 21:25:10 by willem #+# #+# */ /* 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) if (access(path, X_OK) < 0)
{ {
error_msg(path, "Permission denied"); error_msg(path, "Permission denied");
return (0); return (127);
} }
if (is_dir(path)) if (is_dir(path))
{ {
error_msg(path, "Is a directory"); 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; char *path;
int exit_status;
if (command->fd_in != 0) if (command->fd_in != 0)
dup2(command->fd_in, 0); dup2(command->fd_in, 0);
@ -53,9 +54,11 @@ void executor_child(t_minishell *msh, t_command *command)
{ {
errno = 0; errno = 0;
error_msg("minishell", "command not found"); error_msg("minishell", "command not found");
return ; return (127);
} }
else if (! validate_executable_path(path)) exit_status = validate_executable_path(path);
return ; if (exit_status != 0)
return (exit_status) ;
execve(path, command->args, environment_get_arr(msh)); execve(path, command->args, environment_get_arr(msh));
return (126);
} }

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/12 21:24:52 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 executor_fork(t_minishell *msh, t_command *command)
{ {
pid_t pid; pid_t pid;
int exit_status;
pid = fork(); pid = fork();
if (pid > 0) if (pid > 0)
@ -29,8 +30,8 @@ pid_t executor_fork(t_minishell *msh, t_command *command)
else if (pid == 0) else if (pid == 0)
{ {
signal_init_child(); signal_init_child();
executor_child(msh, command); exit_status = executor_child(msh, command);
exit(127); exit(exit_status);
} }
else else
error_msg("minishell", "fork failed"); error_msg("minishell", "fork failed");

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* main.c :+: :+: :+: */ /* main.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ /* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
/* Updated: 2025/03/05 15:17:58 by qmennen ### ########.fr */ /* 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) int main(int argc, char **argv, char **envp)
{ {
t_minishell *msh; t_minishell *msh;
int exit_status;
print_banner(); print_banner();
history_load(); history_load();
@ -63,7 +64,8 @@ int main(int argc, char **argv, char **envp)
main_test(msh, argv[2]); main_test(msh, argv[2]);
else else
main_loop(msh); main_loop(msh);
exit_status = msh->exit_status;
free_minishell(&msh); free_minishell(&msh);
rl_clear_history(); rl_clear_history();
return (EXIT_SUCCESS); return (exit_status);
} }