From 1aebf7531a4508cd011cabb74304e43378138ee6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 24 Mar 2017 20:11:24 +0100 Subject: [PATCH] redirection while works --- 42sh/includes/exec.h | 6 +++-- 42sh/libft/Makefile | 1 + 42sh/libft/includes/libft.h | 2 +- 42sh/libft/includes/lst.h | 3 ++- 42sh/libft/src/lst/pop.c | 2 +- 42sh/libft/src/lst/top.c | 18 ++++++++++++++ 42sh/libft/src/sys/dup2_close.c | 6 ++--- 42sh/libft/src/sys/fd_replace.c | 6 +++-- 42sh/src/exec/exec_leaf.c | 3 +-- 42sh/src/exec/exec_reset.c | 33 ++++++++++++++++++++----- 42sh/src/exec/process_launch.c | 41 ++++++++++++++++++-------------- 42sh/src/exec/redirect_great.c | 9 ++++--- 42sh/src/lexer/get_lexer_stack.c | 2 +- 42sh/src/main/main.c | 2 +- 42sh/src/main/shell_reset.c | 8 ++++--- 15 files changed, 98 insertions(+), 44 deletions(-) create mode 100644 42sh/libft/src/lst/top.c diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 2d24813b..1bf1074b 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/03/24 14:47:12 by ariard ### ########.fr */ +/* Updated: 2017/03/24 20:07:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -131,7 +131,7 @@ struct s_job struct s_exec { t_job job; - int fd_save[10]; + t_list *fd_save[10]; t_flag attrs; int fdin; t_list *op_stack; @@ -140,6 +140,8 @@ struct s_exec }; int exec_reset(void); +int exec_pushfds(); +int exec_popfds(); int process_fork(t_process *p); int process_setgroup(t_process *p, pid_t pid); void process_setsig(void); diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 48456c09..30571112 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -108,6 +108,7 @@ lst/ft_lstnew_range.c\ lst/ft_lstsort.c\ lst/pop.c\ lst/push.c\ +lst/top.c\ math/ft_addrcmp.c\ math/ft_ilen.c\ math/ft_ilen_base.c\ diff --git a/42sh/libft/includes/libft.h b/42sh/libft/includes/libft.h index 98477d16..c9baf04d 100644 --- a/42sh/libft/includes/libft.h +++ b/42sh/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/03/21 16:32:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:08:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/includes/lst.h b/42sh/libft/includes/lst.h index 166cf985..2b6a9e24 100644 --- a/42sh/libft/includes/lst.h +++ b/42sh/libft/includes/lst.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */ -/* Updated: 2017/03/11 16:18:00 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:09:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ typedef struct s_list t_list; int pop(t_list **lst); t_list *push(t_list **stack, int elem); +int top(t_list *top); t_list *ft_lstnew(void const *content, size_t content_size); void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); diff --git a/42sh/libft/src/lst/pop.c b/42sh/libft/src/lst/pop.c index 148d6d7c..df3be22e 100644 --- a/42sh/libft/src/lst/pop.c +++ b/42sh/libft/src/lst/pop.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/04 11:09:56 by jhalford #+# #+# */ -/* Updated: 2017/03/03 16:49:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:05:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/lst/top.c b/42sh/libft/src/lst/top.c new file mode 100644 index 00000000..2c3186e2 --- /dev/null +++ b/42sh/libft/src/lst/top.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* top.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 20:04:27 by jhalford #+# #+# */ +/* Updated: 2017/03/24 20:09:06 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int top(t_list *top) +{ + return (top ? *(int*)top->content : 0); +} diff --git a/42sh/libft/src/sys/dup2_close.c b/42sh/libft/src/sys/dup2_close.c index 5a7924a6..c1c10bcd 100644 --- a/42sh/libft/src/sys/dup2_close.c +++ b/42sh/libft/src/sys/dup2_close.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/07 16:07:28 by jhalford #+# #+# */ -/* Updated: 2017/02/07 16:15:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 19:33:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,9 @@ int dup2_close(int fd1, int fd2) { - if (dup2(fd1, fd2)) + if (dup2(fd1, fd2) < 0) return (-1); - if (close(fd1)) + if (close(fd1) < 0) return (-1); return (0); } diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c index aac43597..6af06a9e 100644 --- a/42sh/libft/src/sys/fd_replace.c +++ b/42sh/libft/src/sys/fd_replace.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/21 16:32:26 by jhalford #+# #+# */ -/* Updated: 2017/03/24 19:09:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:08:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,10 @@ int fd_replace(int fd1, int fd2) { - DG("%i ----> %i", fd2, fd1); if (fd1 != fd2) + { + DG("%i ---> %i", fd2, fd1); return (dup2_close(fd1, fd2)); + } return (0); } diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index 5efe775d..88ac6da0 100644 --- a/42sh/src/exec/exec_leaf.c +++ b/42sh/src/exec/exec_leaf.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:47:30 by wescande #+# #+# */ -/* Updated: 2017/03/24 19:14:08 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 19:24:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,6 @@ int exec_leaf(t_btree **ast) job_addprocess(&p); if (IS_PIPEEND(p)) { - DG("end of pipe"); if (JOB_IS_FG(job->attrs)) put_job_in_foreground(job, 0); else diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index 36ea6ae0..07ab4d0b 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/24 18:41:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:09:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,17 +22,38 @@ int exec_reset_job(t_job *job) return (0); } +int exec_pushfds() +{ + int i; + t_exec *exec; + + exec = &data_singleton()->exec; + i = -1; + while (++i < 10) + push(&(exec->fd_save[i]), fcntl(i, F_DUPFD_CLOEXEC, 10)); + return (0); +} + +int exec_popfds() +{ + int i; + t_exec *exec; + + exec = &data_singleton()->exec; + i = -1; + while (++i < 10) + pop(&exec->fd_save[i]); + return (0); +} + int exec_reset(void) { - t_exec *exec; t_jobc *jobc; - int i; + t_exec *exec; exec = &data_singleton()->exec; jobc = &data_singleton()->jobc; - i = -1; - while (++i < 10) - exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10); + exec_pushfds(); exec->op_stack = NULL; exec->fdin = STDIN; exec->attrs = 0; diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index ef7724fc..ac107417 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/24 19:21:07 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:02:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,23 +35,28 @@ int process_fork(t_process *p) int process_launch(t_process *p) { p->state = PROCESS_RUNNING; - if (IS_PIPESINGLE(*p) - && p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL) + if (!IS_PIPESINGLE(*p) + || p->type == PROCESS_FILE + || p->type == PROCESS_SUBSHELL) { - if (process_redirect(p)) - set_exitstatus(1, 1); - else - p->map.launch(p); - shell_resetfds(); - shell_resetsig(); - process_free(p, 0); - return (0); + p->pid = process_fork(p); + process_setgroup(p, p->pid); + if (p->fdin != STDIN) + close(p->fdin); + if (p->fdout != STDOUT) + close(p->fdout); + return (1); } - p->pid = process_fork(p); - process_setgroup(p, p->pid); - if (p->fdin != STDIN) - close(p->fdin); - if (p->fdout != STDOUT) - close(p->fdout); - return (1); + if (process_redirect(p)) + set_exitstatus(1, 1); + else + { + exec_pushfds(); + p->map.launch(p); + } + exec_popfds(); + shell_resetfds(); + shell_resetsig(); + process_free(p, 0); + return (0); } diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index 5c39be5e..8f4fe029 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ -/* Updated: 2017/03/24 18:25:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:09:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,10 @@ int redirect_great(t_redir *redir) fdnew = redir->n; if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) - exit(1); - dup2_close(fdold, fdnew); + { + SH_ERR("open(): %s", strerror(errno)); + exit (1); + } + fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/lexer/get_lexer_stack.c b/42sh/src/lexer/get_lexer_stack.c index a3c91de3..8e04ad7a 100644 --- a/42sh/src/lexer/get_lexer_stack.c +++ b/42sh/src/lexer/get_lexer_stack.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/03 17:39:45 by jhalford #+# #+# */ -/* Updated: 2017/03/16 22:50:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:04:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 5788f10e..d96b37c6 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/24 19:23:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:02:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_reset.c b/42sh/src/main/shell_reset.c index 125f30c6..dca33bfd 100644 --- a/42sh/src/main/shell_reset.c +++ b/42sh/src/main/shell_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/22 16:07:14 by jhalford #+# #+# */ -/* Updated: 2017/03/22 17:49:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:09:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,12 +26,14 @@ void shell_resetfds(void) { t_exec *exec; int i; + int fd; exec = &data_singleton()->exec; i = -1; while (++i < 10) { - if (exec->fd_save[i] != -1) - dup2(exec->fd_save[i], i); + fd = top(exec->fd_save[i]); + if (fd != -1) + dup2(fd, i); } }