diff --git a/inc/typedef.h b/inc/typedef.h index 33268c2..7eacab1 100644 --- a/inc/typedef.h +++ b/inc/typedef.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */ -/* Updated: 2025/03/04 18:11:00 by whaffman ######## odam.nl */ +/* Updated: 2025/03/19 13:46:51 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -66,6 +66,7 @@ typedef struct s_command int fd_in; int fd_out; int n_fds; + int pid; int exit_status; } t_command; diff --git a/src/executor/executor_execute_pipeline.c b/src/executor/executor_execute_pipeline.c index bb3eda4..302649b 100644 --- a/src/executor/executor_execute_pipeline.c +++ b/src/executor/executor_execute_pipeline.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:02 by willem #+# #+# */ -/* Updated: 2025/03/07 16:17:26 by whaffman ######## odam.nl */ +/* Updated: 2025/03/19 14:03:51 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -48,11 +48,33 @@ static int executor_execute_command(t_minishell *msh, t_command *cmd) return (0); } +static void executor_wait(t_minishell*msh) +{ + t_list *cur; + int exit_status; + + exit_status = 0; + cur = msh->commands; + while (cur) + { + if (((t_command *)cur->content)->pid == 0) + { + cur = cur->next; + continue ; + } + waitpid(((t_command *)cur->content)->pid, &exit_status, 0); + if (WIFEXITED(exit_status)) + msh->exit_status = WEXITSTATUS(exit_status); + else + msh->exit_status = WTERMSIG(exit_status) + 128; + cur = cur->next; + } +} + int executor_execute_pipeline(t_minishell *msh) { t_list *current; pid_t last_pid; - int exit_status; executor_create_pipes(msh); executor_create_redirects(msh); @@ -64,11 +86,7 @@ int executor_execute_pipeline(t_minishell *msh) last_pid = executor_execute_command(msh, (t_command *)current->content); current = current->next; } - if (last_pid != 0) - { - waitpid(last_pid, &exit_status, 0); - msh->exit_status = ((exit_status) & 0xff00) >> 8; - } + executor_wait(msh); signal_init_minishell(); return (msh->exit_status); } diff --git a/src/executor/executor_fork.c b/src/executor/executor_fork.c index 8a604d2..5efcc53 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/03/06 11:34:43 by whaffman ######## odam.nl */ +/* Updated: 2025/03/19 13:58:33 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ pid_t executor_fork(t_minishell *msh, t_command *command) pid = fork(); if (pid > 0) { + command->pid = pid; if (command->fd_in != 0) close(command->fd_in); if (command->fd_out != 1) diff --git a/src/parser/parser_alloc_command.c b/src/parser/parser_alloc_command.c index ecd7e1c..d25f46b 100644 --- a/src/parser/parser_alloc_command.c +++ b/src/parser/parser_alloc_command.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */ -/* Updated: 2025/03/04 18:18:09 by whaffman ######## odam.nl */ +/* Updated: 2025/03/19 13:47:25 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,5 +23,6 @@ t_command *parser_alloc_command(t_minishell *msh) command->redirect_in = NULL; command->redirect_out = NULL; command->n_fds = 0; + command->pid = 0; return (command); }