diff options
Diffstat (limited to 'src/ft_e_externs_pipes.c')
-rw-r--r-- | src/ft_e_externs_pipes.c | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/src/ft_e_externs_pipes.c b/src/ft_e_externs_pipes.c index e5368f2..df460df 100644 --- a/src/ft_e_externs_pipes.c +++ b/src/ft_e_externs_pipes.c @@ -16,6 +16,7 @@ #include <stdint.h> #include <unistd.h> +#include "ft_d_define.h" #include "ft_e_externs_next.h" #include "ft_m_redirs.h" #include "ft_s_destroy.h" @@ -38,37 +39,39 @@ static void t_msh *msh) { /* TODO: norme */ - pid_t pid[2]; + pid_t pid; int32_t pipefd[2]; int32_t status; pipe(pipefd); - if ((pid[0] = fork()) == 0) + if ((pid = fork()) == 0) { - close(pipefd[0]); - dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[FT_WRITE_END]); + dup2(pipefd[FT_READ_END], STDOUT_FILENO); + close(pipefd[FT_READ_END]); ft_e_extern_child(fullpath[0], ptr->one, msh); } - else if (pid[0] < 0) + else if (pid < 0) { /* TODO: handle fork failed */ } else { - if ((pid[1] = fork()) == 0) + if ((pid = fork()) == 0) { - close(pipefd[1]); - dup2(pipefd[0], STDIN_FILENO); + dup2(pipefd[FT_READ_END], STDIN_FILENO); + close(pipefd[FT_WRITE_END]); + close(pipefd[FT_WRITE_END]); ft_e_extern_child(fullpath[1], ptr->next->one, msh); } - else if (pid[1] < 0) + else if (pid < 0) { /* TODO: handle fork failed */ } - close(pipefd[0]); - close(pipefd[1]); - waitpid(pid[1], &status, 0); - waitpid(pid[0], &status, 0); + close(pipefd[FT_READ_END]); + close(pipefd[FT_WRITE_END]); + waitpid(-1, &status, 0); + waitpid(-1, &status, 0); msh->ret = WEXITSTATUS(status); } } @@ -77,28 +80,30 @@ void ft_e_externs_pipes(struct s_lpipes *ptr, t_msh *msh) { - char **envpath; - char **fullpath; - size_t i; + struct s_lpipes *p_ptr; + char **envpath; + char **fullpath; + size_t i; + p_ptr = ptr; if (!(fullpath = (char**)malloc(3 * sizeof(char*)))) return ; fullpath[2] = NULL; i = 0; while (i < 2) { - if (ft_ischarset("/.", ptr->one->com[0])) + if (ft_ischarset("/.", p_ptr->one->com[0])) { - if (!(fullpath[i] = ft_strdup(ptr->one->com))) + if (!(fullpath[i] = ft_strdup(p_ptr->one->com))) return ; } else if ((envpath = ft_get_env_path(msh)) != NULL) { - fullpath[i] = ft_search_in_path(ptr->one->com, envpath, msh); + fullpath[i] = ft_search_in_path(p_ptr->one->com, envpath, msh); ft_delwords(envpath); } /* TODO: deal if not found etc */ - ptr = ptr->next; + p_ptr = p_ptr->next; i++; } ft_exec_path((const char**)fullpath, ptr, msh); |