/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* 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_d_define.h" #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; int32_t pipefd[2]; int32_t status; pipe(pipefd); if ((pid = fork()) == 0) { dup2(pipefd[FT_WRITE_END], STDOUT_FILENO); close(pipefd[FT_READ_END]); close(pipefd[FT_WRITE_END]); ft_e_extern_child(fullpath[0], ptr->one, msh); } else if (pid < 0) { /* TODO: handle fork failed */ } else { if ((pid = fork()) == 0) { dup2(pipefd[FT_READ_END], STDIN_FILENO); close(pipefd[FT_WRITE_END]); close(pipefd[FT_READ_END]); ft_e_extern_child(fullpath[1], ptr->next->one, msh); } else if (pid < 0) { /* TODO: handle fork failed */ } else { close(pipefd[FT_READ_END]); close(pipefd[FT_WRITE_END]); waitpid(pid, &status, 0); } waitpid(pid, &status, 0); msh->ret = WEXITSTATUS(status); } } void ft_e_externs_pipes(struct s_lpipes *ptr, t_msh *msh) { 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("/.", p_ptr->one->com[0])) { 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(p_ptr->one->com, envpath, msh); ft_delwords(envpath); } /* TODO: deal if not found etc */ p_ptr = p_ptr->next; i++; } ft_exec_path((const char**)fullpath, ptr, msh); ft_delwords(fullpath); }