diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-10-05 15:43:33 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-10-05 15:43:33 +0200 |
commit | 7c82cd4d8f2d78adca2db968edfec2381db108df (patch) | |
tree | 90b7e1886134b614d73dfc26a18af29655921a6a /src/e_pipes.c | |
parent | Pipes rework incomming (diff) | |
download | 42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.tar.gz 42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.tar.bz2 42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.tar.xz 42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.tar.zst 42-minishell-7c82cd4d8f2d78adca2db968edfec2381db108df.zip |
pipes update
Diffstat (limited to 'src/e_pipes.c')
-rw-r--r-- | src/e_pipes.c | 107 |
1 files changed, 51 insertions, 56 deletions
diff --git a/src/e_pipes.c b/src/e_pipes.c index 1b4e314..0177870 100644 --- a/src/e_pipes.c +++ b/src/e_pipes.c @@ -14,6 +14,7 @@ #include <libft.h> #include <stdlib.h> #include <stdint.h> +#include <limits.h> #include <unistd.h> #include "e_builtins.h" @@ -24,23 +25,50 @@ #include "u_utils.h" #include "u_path.h" +static uint8_t e_get_current_path(char fullpath[], + struct s_lpipes *ptr, + t_msh *msh) +{ + uint8_t fp_ret; + + fp_ret = 0; + if (ptr->com->bin != NULL && ft_ischarset("/.", ptr->com->bin[0]) == TRUE) + { + ft_strlcpy(fullpath, ptr->com->bin, PATH_MAX); + } + else if (ptr->com->bin != NULL) + { + if (u_get_builtin_id(ptr->com->bin) < B_BUILTINS_COUNT) + { + ft_strlcpy(fullpath, "builtin", 8); + } + else + { + fp_ret = u_search_in_path(fullpath, ptr->com->bin, PATH_MAX, msh); + } + } + return (fp_ret); +} + /* ** TODO: handle fork() failed, etc */ static int32_t e_unroll_pipes(int32_t fd[256][2], - char *fullpath[], size_t pipes, t_msh *msh) { struct s_lpipes *head; + char fullpath[PATH_MAX]; int32_t pid; uint16_t i; + uint8_t fp_ret; head = msh->pipes; i = 0; while (i <= pipes && i < 255) { + fp_ret = e_get_current_path(fullpath, head, msh); if ((pid = fork()) == 0) { if (i != 0) @@ -48,7 +76,7 @@ static int32_t e_unroll_pipes(int32_t fd[256][2], if (i != pipes) dup2(fd[i][E_READ_END], STDOUT_FILENO); e_close_unused_fds(fd, pipes); - e_pipe_child(fullpath, i, head->com, msh); + e_pipe_child(fullpath, fp_ret, head->com, msh); } head = head->next; i++; @@ -60,9 +88,7 @@ static int32_t e_unroll_pipes(int32_t fd[256][2], ** TODO: error mgmnt */ -static void e_pipe_exec_path(char *fullpath[], - size_t pipes, - t_msh *msh) +static void e_pipe_exec(size_t pipes, t_msh *msh) { int32_t fd[256][2]; int32_t pid; @@ -75,7 +101,7 @@ static void e_pipe_exec_path(char *fullpath[], pipe(fd[i]); i++; } - pid = e_unroll_pipes(fd, fullpath, pipes, msh); + pid = e_unroll_pipes(fd, pipes, msh); i = 0; while (i < pipes && i < 255) { @@ -97,61 +123,30 @@ static void e_pipe_exec_path(char *fullpath[], /* exit(127); */ /* } */ -static char *e_get_current_path(struct s_lpipes *rptr, t_msh *msh) -{ - char tmp[PATH_MAX]; - char *path; - uint8_t fp_ret; - - path = NULL; - if (rptr->com->bin != NULL && ft_ischarset("/.", rptr->com->bin[0]) == TRUE) - { - if ((path = ft_strdup(rptr->com->bin)) == NULL) - f_alloc_and_destroy_msh(msh); - } - else - { - if (u_get_builtin_id(rptr->com->bin) < B_BUILTINS_COUNT) - { - if ((path = ft_strdup("builtin")) == NULL) - f_alloc_and_destroy_msh(msh); - } - else - { - fp_ret = u_search_in_path(tmp, rptr->com->bin, PATH_MAX, msh); - if ((path = ft_strdup(tmp)) == NULL) - f_alloc_and_destroy_msh(msh); - } - } - return (path); -} - -static char **e_get_fullpath(size_t pipes, t_msh *msh) -{ - struct s_lpipes *rptr; - char **fullpath; - size_t i; +/* static char **e_get_fullpath(size_t pipes, t_msh *msh) */ +/* { */ +/* struct s_lpipes *rptr; */ +/* char **fullpath; */ +/* size_t i; */ - rptr = msh->pipes; - if ((fullpath = (char **)malloc((pipes + 2) * sizeof(char *))) == NULL) - f_alloc_and_destroy_msh(msh); - fullpath[pipes + 1] = NULL; - i = 0; - while (rptr != NULL) - { - fullpath[i] = e_get_current_path(rptr, msh); - i++; - rptr = rptr->next; - } - return (fullpath); -} +/* rptr = msh->pipes; */ +/* if ((fullpath = (char **)malloc((pipes + 2) * sizeof(char *))) == NULL) */ +/* f_alloc_and_destroy_msh(msh); */ +/* fullpath[pipes + 1] = NULL; */ +/* i = 0; */ +/* while (rptr != NULL) */ +/* { */ +/* fullpath[i] = e_get_current_path(rptr, msh); */ +/* i++; */ +/* rptr = rptr->next; */ +/* } */ +/* return (fullpath); */ +/* } */ void e_pipes(t_msh *msh) { const size_t pipes = e_get_pipes_count(msh->pipes); - fullpath = e_get_fullpath(pipes, msh); - e_pipe_exec_path(fullpath, pipes, msh); - ft_delwords(fullpath); + e_pipe_exec(pipes, msh); s_lpipes_clear(&msh->pipes); } |