From d7f5116eb063b4dd7b20135910db7d229bfa2714 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 7 Feb 2017 02:07:47 +0100 Subject: [PATCH] looks promising --- 42sh/11 | 16 ------ 42sh/9- | 17 ------- 42sh/Makefile | 2 +- 42sh/file | 7 +-- 42sh/includes/exec.h | 23 ++++++--- 42sh/includes/types.h | 1 + ...process_do_redirection.c => fd_is_valid.c} | 49 +------------------ 42sh/src/exec/process_redirect.c | 30 +++++++++++- 42sh/src/exec/redirect_dgreat.c | 10 ++-- 42sh/src/exec/redirect_great.c | 11 +++-- 42sh/src/exec/redirect_greatand.c | 10 ++-- 42sh/src/exec/redirect_less.c | 11 +++-- 42sh/src/exec/redirect_lessand.c | 11 +++-- 13 files changed, 85 insertions(+), 113 deletions(-) delete mode 100644 42sh/11 delete mode 100644 42sh/9- rename 42sh/src/exec/{process_do_redirection.c => fd_is_valid.c} (50%) diff --git a/42sh/11 b/42sh/11 deleted file mode 100644 index 367e9eae..00000000 --- a/42sh/11 +++ /dev/null @@ -1,16 +0,0 @@ -11 -42sh -Makefile -Session.vim -a.out -debug -donovan_segaults_06-02 -file -includes -libft -objs -out -pdf -script.sh -src -update_makefile.sh diff --git a/42sh/9- b/42sh/9- deleted file mode 100644 index 62cc79f8..00000000 --- a/42sh/9- +++ /dev/null @@ -1,17 +0,0 @@ -11 -42sh -9- -Makefile -Session.vim -a.out -debug -donovan_segaults_06-02 -file -includes -libft -objs -out -pdf -script.sh -src -update_makefile.sh diff --git a/42sh/Makefile b/42sh/Makefile index 9628207c..ad0bbca4 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -61,10 +61,10 @@ exec/exec_or_if.c\ exec/exec_pipe.c\ exec/exec_redir.c\ exec/exec_semi.c\ +exec/fd_is_valid.c\ exec/ft_exec.c\ exec/ft_findexec.c\ exec/launch_process.c\ -exec/process_do_redirection.c\ exec/process_redirect.c\ exec/process_reset.c\ exec/process_setexec.c\ diff --git a/42sh/file b/42sh/file index 62cc79f8..982f231a 100644 --- a/42sh/file +++ b/42sh/file @@ -1,12 +1,13 @@ -11 +4 42sh -9- Makefile Session.vim -a.out debug donovan_segaults_06-02 file +file1 +file2 +file3 includes libft objs diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 609c5936..ead042eb 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -67,9 +67,16 @@ struct s_execmap int (*f)(t_btree **ast); }; +struct s_redirmap +{ + t_flag type; + int (*f)(t_redir *redir); +}; + #include "minishell.h" extern t_execmap g_execmap[]; +extern t_redirmap g_redirmap[]; int ft_exec(t_btree **ast); @@ -88,15 +95,15 @@ void process_setsig(void); void process_free(void *content, size_t content_size); void process_reset(void); -int process_redirect(t_process *p); -int process_do_redirection(t_redir *redir); +int fd_is_valid(int fd); void bad_fd(int fd); -int redirect_great(t_redir *redir, int *fdold, int *fdnew); -int redirect_less(t_redir *redir, int *fdold, int *fdnew); -int redirect_dgreat(t_redir *redir, int *fdold, int *fdnew); -int redirect_dless(t_redir *redir, int *fdold, int *fdnew); -int redirect_greatand(t_redir *redir, int *fdold, int *fdnew); -int redirect_lessand(t_redir *redir, int *fdold, int *fdnew); +int process_redirect(t_process *p); +int redirect_great(t_redir *redir); +int redirect_less(t_redir *redir); +int redirect_dgreat(t_redir *redir); +int redirect_dless(t_redir *redir); +int redirect_greatand(t_redir *redir); +int redirect_lessand(t_redir *redir); char *ft_findexec(char *path, char *file); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 9e86571c..07cec9b7 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -32,6 +32,7 @@ typedef union u_word t_word; typedef struct s_job t_job; typedef struct s_execmap t_execmap; +typedef struct s_redirmap t_redirmap; typedef struct s_process t_process; typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]); diff --git a/42sh/src/exec/process_do_redirection.c b/42sh/src/exec/fd_is_valid.c similarity index 50% rename from 42sh/src/exec/process_do_redirection.c rename to 42sh/src/exec/fd_is_valid.c index cb5f618b..bcb6cdda 100644 --- a/42sh/src/exec/process_do_redirection.c +++ b/42sh/src/exec/fd_is_valid.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* process_do_redirection.c :+: :+: :+: */ +/* fd_is_valid.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -16,50 +16,3 @@ int fd_is_valid(int fd) { return (fcntl(fd, F_GETFD) != -1 || errno != EBADF); } - -int process_do_redirection(t_redir *redir) -{ - int fdold; - int fdnew; - - if (redir->n > 9) - bad_fd(redir->n); - if (redir->type & TK_GREAT) - redirect_great(redir, &fdold, &fdnew); - else if (redir->type & TK_GREAT) - redirect_dgreat(redir, &fdold, &fdnew); - else if (redir->type & TK_LESS) - redirect_less(redir, &fdold, &fdnew); - else if (redir->type & TK_GREATAND) - { - if (redirect_greatand(redir, &fdold, &fdnew)) - return (0); - } - else if (redir->type & TK_LESSAND) - { - if (redirect_lessand(redir, &fdold, &fdnew)) - return (0); - } - else - exit(42); - DG("gonna redirect dup2(%i,%i)", fdold, fdnew); - if (fd_is_valid(fdnew)) - { - if (fd_is_valid(fdold)) - { - dup2(fdold, fdnew); - close(fdold); - } - else - bad_fd(fdold); - } - else - { - DG("[%i] is not a valid fd", fdnew); - if (fdnew <= 2) - close(fdnew); - else - bad_fd(fdnew); - } - return (0); -} diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 9574008b..925a6b92 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -12,14 +12,42 @@ #include "exec.h" +t_redirmap g_redirmap[] = +{ + {TK_LESS, redirect_less}, + {TK_GREAT, redirect_great}, + /* {TK_DLESS, redirect_dless}, */ + {TK_DGREAT, redirect_dgreat}, + {TK_LESSAND, redirect_lessand}, + {TK_GREATAND, redirect_greatand}, + {0, NULL}, +}; + int process_redirect(t_process *p) { t_list *redirs; + t_redir *redir; + int i; redirs = p->redirs; while (redirs) { - process_do_redirection(redirs->content); + i = 0; + redir = redirs->content; + if (redir->n > 9) + { + bad_fd(redir->n); + exit(1); + } + while (g_redirmap[i].type) + { + if (g_redirmap[i].type == redir->type) + { + (g_redirmap[i].f)(redir); + break ; + } + i++; + } redirs = redirs->next; } if (p->toclose != STDIN) diff --git a/42sh/src/exec/redirect_dgreat.c b/42sh/src/exec/redirect_dgreat.c index 485fbda9..62e01058 100644 --- a/42sh/src/exec/redirect_dgreat.c +++ b/42sh/src/exec/redirect_dgreat.c @@ -12,14 +12,18 @@ #include "exec.h" -int redirect_dgreat(t_redir *redir, int *fdold, int *fdnew) +int redirect_dgreat(t_redir *redir) { - *fdold = redir->n; - if ((*fdnew = open(redir->word.word, + int fdold; + int fdnew; + + fdnew = redir->n; + if ((fdold = open(redir->word.word, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) { DG("open errno=%i", errno); exit(1); } + dup2(fdold, fdnew); return (0); } diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index 67aaa003..bd6e170a 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -12,15 +12,18 @@ #include "exec.h" -int redirect_great(t_redir* redir, int *fdold, int *fdnew) +int redirect_great(t_redir* redir) { - *fdnew = redir->n; - if ((*fdold = open(redir->word.word, + int fdold; + int fdnew; + + fdnew = redir->n; + if ((fdold = open(redir->word.word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) { DG("open errno=%i", errno); exit(1); } - return (0); dup2(fdold, fdnew); + return (0); } diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index 162b78b2..1dd2fb4e 100644 --- a/42sh/src/exec/redirect_greatand.c +++ b/42sh/src/exec/redirect_greatand.c @@ -10,11 +10,13 @@ /* */ /* ************************************************************************** */ - #include "exec.h" -int redirect_greatand(t_redir *redir, int *fdold, int *fdnew) +int redirect_greatand(t_redir *redir) { + int fdold; + int fdnew; + if (redir->word.fd > 9) bad_fd(redir->word.fd); if (redir->close) @@ -24,8 +26,8 @@ int redirect_greatand(t_redir *redir, int *fdold, int *fdnew) } else { - *fdnew = redir->n; - *fdold = redir->word.fd; + fdold = redir->word.fd; + fdnew = redir->n; } if (fd_is_valid(fdold)) { diff --git a/42sh/src/exec/redirect_less.c b/42sh/src/exec/redirect_less.c index 5e404390..b68ca76a 100644 --- a/42sh/src/exec/redirect_less.c +++ b/42sh/src/exec/redirect_less.c @@ -12,15 +12,18 @@ #include "exec.h" -int redirect_less(t_redir *redir, int *fdold, int *fdnew) +int redirect_less(t_redir *redir) { - *fdnew = redir->n; - if ((*fdold = open(redir->word.word, O_RDONLY)) < 0) + int fdold; + int fdnew; + + fdnew = redir->n; + if ((fdold = open(redir->word.word, O_RDONLY)) < 0) { ft_dprintf(2, "{red}%s: no such file or directory: %s{eoc}\n", SHELL_NAME, redir->word.word); exit (1); } - return (0); dup2(fdold, fdnew); + return (0); } diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c index 52a4b497..6109abe1 100644 --- a/42sh/src/exec/redirect_lessand.c +++ b/42sh/src/exec/redirect_lessand.c @@ -12,8 +12,11 @@ #include "exec.h" -int redirect_lessand(t_redir *redir, int *fdold, int *fdnew) +int redirect_lessand(t_redir *redir) { + int fdold; + int fdnew; + if (redir->word.fd > 9) bad_fd(redir->word.fd); if (redir->close) @@ -23,10 +26,10 @@ int redirect_lessand(t_redir *redir, int *fdold, int *fdnew) } else { - *fdnew = redir->word.fd; - *fdold = redir->n; + fdold = redir->word.fd; + fdnew = redir->n; } - if (fd_is_valid(fdnew)) + if (fd_is_valid(fdold)) { dup2(fdold, fdnew); close(fdold);