diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | src/ft_e_externs.h | 4 | ||||
-rw-r--r-- | src/ft_e_externs_next.h | 4 | ||||
-rw-r--r-- | src/ft_e_externs_pipes.c | 104 | ||||
-rw-r--r-- | src/ft_e_externs_pipes.h | 20 | ||||
-rw-r--r-- | src/ft_e_pipes.c | 5 |
6 files changed, 131 insertions, 7 deletions
@@ -25,6 +25,7 @@ SRCS_NAME += b_unset SRCS_NAME += e_builtins SRCS_NAME += e_externs SRCS_NAME += e_externs_next +SRCS_NAME += e_externs_pipes SRCS_NAME += e_lcom SRCS_NAME += e_pipes SRCS_NAME += f_chdir diff --git a/src/ft_e_externs.h b/src/ft_e_externs.h index bee3f1e..5e02353 100644 --- a/src/ft_e_externs.h +++ b/src/ft_e_externs.h @@ -10,8 +10,8 @@ /* */ /* ************************************************************************** */ -#ifndef FT_E_EXTERN_H -#define FT_E_EXTERN_H +#ifndef FT_E_EXTERNS_H +#define FT_E_EXTERNS_H #include "ft_s_struct.h" diff --git a/src/ft_e_externs_next.h b/src/ft_e_externs_next.h index 22a37ca..bc6272c 100644 --- a/src/ft_e_externs_next.h +++ b/src/ft_e_externs_next.h @@ -10,8 +10,8 @@ /* */ /* ************************************************************************** */ -#ifndef FT_E_EXTERN_NEXT_H -#define FT_E_EXTERN_NEXT_H +#ifndef FT_E_EXTERNS_NEXT_H +#define FT_E_EXTERNS_NEXT_H #include "ft_s_struct.h" 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); +} diff --git a/src/ft_e_externs_pipes.h b/src/ft_e_externs_pipes.h new file mode 100644 index 0000000..435c795 --- /dev/null +++ b/src/ft_e_externs_pipes.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_e_externs_pipes.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_E_EXTERNS_PIPES_H +#define FT_E_EXTERNS_PIPES_H + +#include "ft_s_struct.h" + +void ft_e_externs_pipes(struct s_lpipes *ptr, t_msh *msh); + +#endif diff --git a/src/ft_e_pipes.c b/src/ft_e_pipes.c index 2ac4bb6..581aec2 100644 --- a/src/ft_e_pipes.c +++ b/src/ft_e_pipes.c @@ -15,7 +15,7 @@ #include <unistd.h> #include "ft_e_builtins.h" -#include "ft_e_externs.h" +#include "ft_e_externs_pipes.h" #include "ft_s_lpipes.h" #include "ft_s_struct.h" @@ -39,12 +39,11 @@ void t_msh *msh) { uint8_t bu_id; - /* int32_t pipefd[2]; */ if ((bu_id = ft_get_builtin_id(ptr->pipes->one->com, msh)) < FT_BUILTINS_COUNT) ft_e_builtin(ptr->pipes->one, bu_id, msh); else - ft_e_extern(ptr->pipes->one, msh); + ft_e_externs_pipes(ptr->pipes, msh); ft_lpipes_clear(&ptr->pipes); } |