diff options
Diffstat (limited to 'src/ft_e_externs_pipes.c')
-rw-r--r-- | src/ft_e_externs_pipes.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/ft_e_externs_pipes.c b/src/ft_e_externs_pipes.c new file mode 100644 index 0000000..d4d648d --- /dev/null +++ b/src/ft_e_externs_pipes.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_e_externs_pipes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <sys/wait.h> +#include <libft.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.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) + { + close(pipefd[0]); + dup2(pipefd[1], STDOUT_FILENO); + ft_e_extern_child(fullpath[0], ptr->one, msh); + } + else if (pid < 0) + { + /* TODO: handle fork failed */ + } + if ((pid = fork()) == 0) + { + close(pipefd[1]); + dup2(pipefd[0], STDIN_FILENO); + ft_e_extern_child(fullpath[1], ptr->next->one, msh); + } + /* else */ + /* { */ + close(pipefd[0]); + close(pipefd[1]); + while (wait(&status) != pid) + ; + while (wait(&status) != pid) + ; + 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); +} |