From a18c4cae82ab1065a14dc02ad3df1d02f79e1234 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Wed, 8 Mar 2023 18:19:10 +0100 Subject: [PATCH] fix: use the bash waitpid implementation --- cmd.c | 2 ++ cmds.c | 4 +--- execution.c | 26 +++++++++++++++----------- main.c | 4 ++-- minishell.h | 1 + 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/cmd.c b/cmd.c index 2decebf..1bdea0f 100644 --- a/cmd.c +++ b/cmd.c @@ -12,6 +12,7 @@ #include "libftx/libftx.h" #include "minishell.h" +#include void ft_cmddel(void *ptr) { @@ -51,5 +52,6 @@ int ft_cmd_filler(t_data *data, t_list *element, char **args) } content->args = args; content->executable = args[0]; + content->pid = -1; return (0); } diff --git a/cmds.c b/cmds.c index 21f4872..21ac594 100644 --- a/cmds.c +++ b/cmds.c @@ -66,9 +66,7 @@ static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile current = *cmds; cmd = current->content; cmd->fd_in = infile; - while (current->next != NULL) - current = current->next; - cmd = current->content; + cmd = ft_lstlast(*cmds)->content; cmd->fd_out = outfile; return (0); } diff --git a/execution.c b/execution.c index 343a2ea..1600952 100644 --- a/execution.c +++ b/execution.c @@ -12,6 +12,7 @@ #include "libftx/libftx.h" #include "minishell.h" +#include #include static char *ft_get_executable_path(t_data *data, char *executable_name) @@ -71,23 +72,21 @@ static char *ft_get_executable_path(t_data *data, char *executable_name) return (path); } -static int ft_executor(t_data *data, t_cmd *cmd) +static void ft_executor(t_data *data, t_cmd *cmd) { int pid; - int return_value; char **tab; - return_value = -1; if (cmd->fd_in == -1 || cmd->fd_out == -1) - return (1); + return ; pid = fork(); if (pid == -1) - return (1); + return ; if (pid == 0) { tab = env_to_strs(data->env); if (tab == NULL) - return (1); + return ; dup2(cmd->fd_out, 1); dup2(cmd->fd_in, 0); if (cmd->fd_out > 2) @@ -96,10 +95,7 @@ static int ft_executor(t_data *data, t_cmd *cmd) close(cmd->fd_in); execve(cmd->executable, cmd->args, tab); } - else - waitpid(pid, &return_value, 0); - data->exit_code = return_value; - return (return_value); + cmd->pid = pid; } static int ft_own_cmd(t_data *data, t_cmd *cmd) @@ -168,7 +164,7 @@ int ft_cmds_executor(t_data *data, t_list **cmds) content->executable = ft_get_executable_path(data, content->executable); if (content->executable != NULL) - exit_code = ft_executor(data, content); + ft_executor(data, content); } else if (exit_code == -2) return (1); @@ -180,5 +176,13 @@ int ft_cmds_executor(t_data *data, t_list **cmds) close(content->fd_out); current = current->next; } + current = *cmds; + while (current != NULL) + { + content = current->content; + if (content->pid != -1) + waitpid(content->pid, &(data->exit_code), 0); + current = current->next; + } return (0); } diff --git a/main.c b/main.c index f1bea15..fb6bac8 100644 --- a/main.c +++ b/main.c @@ -45,10 +45,10 @@ static int ft_minishell(t_data *data, char *line) int outfile; if (ft_syntatic_verif(data, line)) - return (1); + return (0); line_clean = ft_normalizer(line); if (line_clean == NULL) - return (1); + return (0); outfile = ft_outfile(data, line_clean); if (outfile == -2) { diff --git a/minishell.h b/minishell.h index 0b6ee63..111b044 100644 --- a/minishell.h +++ b/minishell.h @@ -91,6 +91,7 @@ typedef struct s_cmd { int fd_in; int fd_out; + int pid; char *executable; char **args; } t_cmd;