diff --git a/.nfs00000000098c82b80000065f b/.nfs00000000098c82b80000065f deleted file mode 100755 index baed54c..0000000 Binary files a/.nfs00000000098c82b80000065f and /dev/null differ diff --git a/Makefile b/Makefile index c97bdba..e8aba2a 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ NAME = minishell CC = clang -CFLAGS = -Werror -Wextra -g +CFLAGS = -Werror -Wextra -Wall -g LIBS = libftx/libftx.a 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 bf4e98b..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,22 +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; 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) @@ -95,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) @@ -167,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); @@ -179,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 9b0ce18..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) { @@ -69,8 +69,6 @@ static int ft_minishell(t_data *data, char *line) close(outfile); if (infile > 2) close(infile); - ft_lstclear(cmds, ft_cmddel); - free(cmds); free(line_clean); return (1); } diff --git a/minishell.h b/minishell.h index b661154..f4d72a3 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; diff --git a/outfile.c b/outfile.c index 35938e8..5bf8c83 100644 --- a/outfile.c +++ b/outfile.c @@ -69,7 +69,7 @@ static int ft_get_outfile(t_data *data, const char *line) ft_eprintf("minishell: malloc failed\n"); return (-2); } - fd = 0; + fd = 1; i = 0; while (tab[i + 1] != NULL) {