/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_e_externs_pipes.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include #include #include #include "ft_e_externs_next.h" #include "ft_m_redirs.h" #include "ft_s_destroy.h" #include "ft_s_lcom.h" #include "ft_s_struct.h" static void ft_e_extern_child(const char *fullpath, t_lcom *ptr, t_msh *msh) { ft_dup_redirs(ptr, msh); execve(fullpath, ptr->argv, msh->envp); /* TODO: handle execve failed */ } static void ft_exec_path(const char *fullpath[], struct s_lpipes *ptr, t_msh *msh) { /* TODO: norme */ pid_t pid[2]; int32_t pipefd[2]; int32_t status; pipe(pipefd); if ((pid[0] = fork()) == 0) { close(pipefd[0]); dup2(pipefd[1], STDOUT_FILENO); ft_e_extern_child(fullpath[0], ptr->one, msh); } else if (pid[0] < 0) { /* TODO: handle fork failed */ } else { if ((pid[1] = fork()) == 0) { close(pipefd[1]); dup2(pipefd[0], STDIN_FILENO); ft_e_extern_child(fullpath[1], ptr->next->one, msh); } else if (pid[1] < 0) { /* TODO: handle fork failed */ } close(pipefd[0]); close(pipefd[1]); waitpid(pid[1], &status, 0); waitpid(pid[0], &status, 0); msh->ret = WEXITSTATUS(status); } } void ft_e_externs_pipes(struct s_lpipes *ptr, t_msh *msh) { char **envpath; char **fullpath; size_t i; if (!(fullpath = (char**)malloc(3 * sizeof(char*)))) return ; fullpath[2] = NULL; i = 0; while (i < 2) { if (ft_ischarset("/.", ptr->one->com[0])) { if (!(fullpath[i] = ft_strdup(ptr->one->com))) return ; } else if ((envpath = ft_get_env_path(msh)) != NULL) { fullpath[i] = ft_search_in_path(ptr->one->com, envpath, msh); ft_delwords(envpath); } /* TODO: deal if not found etc */ ptr = ptr->next; i++; } ft_exec_path((const char**)fullpath, ptr, msh); ft_delwords(fullpath); }