From 156e1cb29c0249962209933853c2e3c14e966da0 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 6 Feb 2017 16:51:34 +0100 Subject: [PATCH] file redirections repaired --- 42sh/includes/exec.h | 4 +-- 42sh/includes/lexer.h | 2 +- 42sh/src/exec/exec_redir.c | 2 +- 42sh/src/exec/ft_exec.c | 2 +- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/exec/process_do_redirection.c | 46 ++++++++++++++++---------- 42sh/src/exec/process_redirect.c | 2 +- 42sh/src/main/main.c | 4 +-- 42sh/src/parser/parse_dgreat.c | 2 +- 42sh/src/parser/parse_dless.c | 2 +- 42sh/src/parser/parse_great.c | 4 +-- 42sh/src/parser/parse_greatand.c | 2 +- 42sh/src/parser/parse_less.c | 6 ++-- 42sh/src/parser/parse_lessand.c | 4 +-- 42sh/src/parser/parse_redir.c | 2 +- 15 files changed, 48 insertions(+), 38 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 0616b685..8519f2af 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:01:10 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 15:54:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,7 +85,7 @@ int launch_process(t_process *p); int process_setexec(t_type type, t_process *p); int process_setgroup(t_process *p, pid_t pid); int process_redirect(t_process *p); -void process_do_redirection(t_redir *redir); +int process_do_redirection(t_redir *redir); void process_setsig(void); void process_free(void *content, size_t content_size); void process_reset(void); diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 10f600a7..29e75b4b 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/02/03 17:31:08 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:22:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_redir.c b/42sh/src/exec/exec_redir.c index d7becf4b..8cb33701 100644 --- a/42sh/src/exec/exec_redir.c +++ b/42sh/src/exec/exec_redir.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2017/02/03 13:58:38 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:12:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 6bf0e751..c1daa008 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/02/03 13:45:05 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 15:27:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 38ff2aaf..2c8577ed 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/02/02 14:28:55 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:08:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_do_redirection.c b/42sh/src/exec/process_do_redirection.c index 2246941d..93726aec 100644 --- a/42sh/src/exec/process_do_redirection.c +++ b/42sh/src/exec/process_do_redirection.c @@ -6,52 +6,62 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:01:18 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:51:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int fd_is_valid(int fd) +int fd_is_valid(int fd) { return (fcntl(fd, F_GETFD) != -1 || errno != EBADF); } -void process_do_redirection(t_redir *redir) +int process_do_redirection(t_redir *redir) { - int fdin; - int fdout; + int fdold; + int fdnew; if (redir->type & (TK_GREAT | TK_DGREAT)) { - fdin = redir->n; - fdout = open(redir->word.word, O_WRONLY | O_CREAT - | (redir->type & TK_GREAT) ? O_TRUNC : O_APPEND, - 0644); + fdold = redir->n; + if ((fdnew = open(redir->word.word, O_WRONLY | O_CREAT + | ((redir->type & TK_GREAT) ? O_TRUNC : O_APPEND), + 0644)) < 0) + { + DG("open errno=%i", errno); + exit(1); + } } else if (redir->type & TK_LESS) { - fdin = open(redir->word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); - fdout = redir->n; + fdold = redir->n; + if ((fdnew = 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); + } } else if (redir->type & (TK_LESSAND | TK_GREATAND)) { if (redir->close) { close(redir->n); - return ; + return (0); } else { - fdin = redir->type & TK_LESSAND ? redir->word.fd : redir->n; - fdout = redir->type & TK_LESSAND ? redir->n : redir->word.fd; + fdold = redir->type & TK_LESSAND ? redir->word.fd : redir->n; + fdnew = redir->type & TK_LESSAND ? redir->n : redir->word.fd; } } else { - ft_dprintf(2, "{red}%s: redirection error.", SHELL_NAME); - return ; + ft_dprintf(2, "{red}%s: redirection error.{eoc}\n", SHELL_NAME); + return (-1); } - fd_is_valid(fdout) ? dup2(fdout, fdin) : close(fdin); - close(fdout); + fd_is_valid(fdold) ? dup2(fdnew, fdold) : close(fdnew); + close(fdnew); + return (0); } diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 4f3396ca..36c3c577 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/02/03 13:46:30 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:46:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 377ad610..30f53822 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/02/06 14:38:45 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:46:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,8 +37,8 @@ int shell_single_command(char *command) int main(int ac, char **av) { setlocale(LC_ALL, ""); - DG("{inv}{bol}{gre}start of shell{eoc} job_control is %s", data_singleton()->opts & SHELL_OPTS_JOBC ? "ON" : "OFF"); shell_init(ac, av); + DG("{inv}{bol}{gre}start of shell{eoc} job_control is %s", data_singleton()->opts & SHELL_OPTS_JOBC ? "ON" : "OFF"); if (data_singleton()->opts & SHELL_OPTS_LC) { shell_single_command(ft_strdup(shell_get_avdata())); diff --git a/42sh/src/parser/parse_dgreat.c b/42sh/src/parser/parse_dgreat.c index dcfe9a5d..df19624b 100644 --- a/42sh/src/parser/parse_dgreat.c +++ b/42sh/src/parser/parse_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:02:52 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:28:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_dless.c b/42sh/src/parser/parse_dless.c index a1970bbb..20d91421 100644 --- a/42sh/src/parser/parse_dless.c +++ b/42sh/src/parser/parse_dless.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 13:31:40 by jhalford #+# #+# */ -/* Updated: 2016/12/03 13:31:41 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:28:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index dffaf1a7..1c6f5dea 100644 --- a/42sh/src/parser/parse_great.c +++ b/42sh/src/parser/parse_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:07:03 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:29:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int parse_great(t_btree **ast, t_list **start, t_list **lst) next_tok = (*lst)->next->content; if (!(next_tok->type & TK_WORD)) return (1); - node->data.redir.n = ft_atoi(tok->data); + node->data.redir.n = *tok->data == '>' ? 1 : ft_atoi(tok->data); node->data.redir.word.word = ft_strdup(next_tok->data); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); diff --git a/42sh/src/parser/parse_greatand.c b/42sh/src/parser/parse_greatand.c index 7751fb83..fdfbd6ff 100644 --- a/42sh/src/parser/parse_greatand.c +++ b/42sh/src/parser/parse_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:09:50 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:30:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_less.c b/42sh/src/parser/parse_less.c index b1da5d1f..53e47790 100644 --- a/42sh/src/parser/parse_less.c +++ b/42sh/src/parser/parse_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/12/05 11:58:48 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:31:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,9 +24,9 @@ int parse_less(t_btree **ast, t_list **start, t_list **lst) return (1); tok = (*lst)->content; next_tok = (*lst)->next->content; - if (next_tok->type != TK_WORD) + if (!(next_tok->type & TK_WORD)) return (1); - node->data.redir.n = ft_atoi(tok->data); + node->data.redir.n = *tok->data == '>' ? 0 : ft_atoi(tok->data); node->data.redir.word.word = ft_strdup(next_tok->data); ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free); ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free); diff --git a/42sh/src/parser/parse_lessand.c b/42sh/src/parser/parse_lessand.c index 2a047610..77e17890 100644 --- a/42sh/src/parser/parse_lessand.c +++ b/42sh/src/parser/parse_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:07:55 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:30:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int parse_lessand(t_btree **ast, t_list **start, t_list **lst) node->type = TK_LESSAND; tok = (*lst)->content; and = ft_strchr(tok->data, '&'); - node->data.redir.n = *tok->data == '<' ? 1 : ft_atoi(tok->data); + node->data.redir.n = *tok->data == '<' ? 0 : ft_atoi(tok->data); node->data.redir.word.fd = ft_atoi(and + 1); node->data.redir.close = tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; diff --git a/42sh/src/parser/parse_redir.c b/42sh/src/parser/parse_redir.c index 95625fd0..4d209f62 100644 --- a/42sh/src/parser/parse_redir.c +++ b/42sh/src/parser/parse_redir.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/02 18:58:27 by jhalford #+# #+# */ -/* Updated: 2017/02/03 13:58:57 by jhalford ### ########.fr */ +/* Updated: 2017/02/06 16:07:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */