From 6b67ceb22416753e5872be46dcdc75ab238e18ac Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 27 Mar 2017 16:47:08 +0200 Subject: [PATCH] process_launch refactor --- 42sh/Makefile | 4 ++-- 42sh/src/exec/plaunch_file.c | 6 +++--- 42sh/src/exec/process_launch.c | 25 +++++++++++-------------- 42sh/src/exec/redirect_great.c | 4 ++-- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/42sh/Makefile b/42sh/Makefile index 91ecaa0b..b39cae83 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,14 +6,14 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/27 15:09:22 by ariard ### ########.fr # +# Updated: 2017/03/27 16:09:37 by jhalford ### ########.fr # # # # **************************************************************************** # NAME = 42sh CC = gcc -FLAGS = -Wall -Wextra -Werror -fsanitize=address +FLAGS = -Wall -Wextra -Werror #-fsanitize=address D_FLAGS = -g DELTA = $$(echo "$$(tput cols)-47"|bc) diff --git a/42sh/src/exec/plaunch_file.c b/42sh/src/exec/plaunch_file.c index 0c66bee9..474bb30d 100644 --- a/42sh/src/exec/plaunch_file.c +++ b/42sh/src/exec/plaunch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/27 16:02:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 16:46:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,8 +28,8 @@ int plaunch_file(t_process *p) error_launch(p->data.cmd.av[0], ": is a directory", 126); else if (access(p->data.cmd.path, X_OK) == -1) error_launch("permission denied: ", p->data.cmd.av[0], 126); - execve( - p->data.cmd.path, + shell_fds_destroy(); + execve(p->data.cmd.path, p->data.cmd.av, data_singleton()->env); error_launch("internal execve error on ", p->data.cmd.av[0], 42); diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index ad08a3b9..9e82b20b 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/27 16:04:22 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 16:46:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,25 +16,22 @@ int process_fork(t_process *p) { pid_t pid; + if (!p) + return (0); if ((pid = fork()) == -1) exit(SH_ERR("fork(): %s", strerror(errno))); - else if (pid) + else if (pid != 0) return (pid); - if (!p) - return (pid); - if (process_redirect(p)) - exit(1); - process_setgroup(p, 0); - process_setsig(); exec_destroy(&data_singleton()->exec); jobc_destroy(&data_singleton()->jobc); - if (p->type == PROCESS_FILE) - shell_fds_destroy(); - else + if ((pid = 1) && process_redirect(p) == 0) + { shell_fds_push(); - data_singleton()->opts &= ~SH_INTERACTIVE; - data_singleton()->opts &= ~SH_OPTS_JOBC; - pid = p->map.launch(p); + process_setgroup(p, 0); + process_setsig(); + data_singleton()->opts &= ~(SH_INTERACTIVE | SH_OPTS_JOBC); + pid = p->map.launch(p); + } shell_fds_destroy(); exit(pid); } diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index bea4de8d..1ea9124d 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/26 22:36:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 16:40:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int redirect_great(t_redir *redir) if ((try_access(redir->word, 0, W_OK))) return (ft_perror(NULL)); if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) - exit (1); + exit(1); fd_replace(fdold, fdnew); return (0); }