From 8a46d9fe2a4f72853698dd79f821585bba46cbcd Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 8 Mar 2017 12:44:09 +0100 Subject: [PATCH] issue #48 first step --- 42sh/includes/exec.h | 4 ++-- 42sh/src/exec/launch_file.c | 9 +++++++-- 42sh/src/exec/process_setexec.c | 14 +++++++++++--- 42sh/src/exec/redir_free.c | 6 +----- 42sh/src/job-control/process_free.c | 6 +++--- 42sh/src/main/main.c | 2 +- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 372c4ef0..e8c72bc6 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/08 03:25:53 by wescande ### ########.fr */ +/* Updated: 2017/03/08 12:26:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,7 @@ struct s_data_cmd char **av; char *path; t_execf *execf; - struct stat stat; + struct stat *stat; }; struct s_data_cond diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c index afe5cb81..56b06a04 100644 --- a/42sh/src/exec/launch_file.c +++ b/42sh/src/exec/launch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/07 21:33:56 by wescande ### ########.fr */ +/* Updated: 2017/03/08 12:42:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,12 @@ int launch_file(t_process *p) ft_dprintf(2, "{red}%s: command not found: %s{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); exit(127); } - else if (S_ISDIR(p->data.cmd.stat.st_mode)) + else if (!p->data.cmd.stat) + { + ft_dprintf(2, "{red}%s: %s: no such file or directory\n", SHELL_NAME, p->data.cmd.av[0]); + exit(127); + } + else if (S_ISDIR(p->data.cmd.stat->st_mode)) { ft_dprintf(2, "{red}%s: %s: Is a directory{eoc}\n", SHELL_NAME, p->data.cmd.av[0]); exit(126); diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 08964e53..8d10402f 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/03/08 03:32:49 by wescande ### ########.fr */ +/* Updated: 2017/03/08 12:42:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ int process_setexec(t_process *p) t_btree *func; p->data.cmd.path = NULL; p->data.cmd.execf = NULL; + p->data.cmd.stat = ft_memalloc(sizeof(struct stat)); + DG("gonna setexec av[0]=[%s]", p->data.cmd.av[0]); if ((func = is_function(p))) { p->data.subshell.content = func; @@ -29,7 +31,8 @@ int process_setexec(t_process *p) p->type = PROCESS_FILE; p->data.cmd.execf = &execve; p->data.cmd.path = ft_strdup(p->data.cmd.av[0]); - stat(p->data.cmd.path, &p->data.cmd.stat); + if (stat(p->data.cmd.path, p->data.cmd.stat) == -1) + ft_memdel((void**)&p->data.cmd.stat); } else { @@ -37,7 +40,12 @@ int process_setexec(t_process *p) if (ft_hash(p)) { p->data.cmd.execf = &execve; - stat(p->data.cmd.path, &p->data.cmd.stat); + DG("found hash at [%s]", p->data.cmd.path); + if (stat(p->data.cmd.path, p->data.cmd.stat) == -1) + { + ft_memdel((void**)&p->data.cmd.stat); + ft_dprintf(2, "{red}%s: %s: unexpected stat (2) failure\n", SHELL_NAME, p->data.cmd.path); + } } } return (0); diff --git a/42sh/src/exec/redir_free.c b/42sh/src/exec/redir_free.c index fa19403f..54f40a65 100644 --- a/42sh/src/exec/redir_free.c +++ b/42sh/src/exec/redir_free.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/03 18:12:57 by ariard #+# #+# */ -/* Updated: 2017/03/05 18:06:24 by ariard ### ########.fr */ +/* Updated: 2017/03/08 12:40:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,9 +22,5 @@ void redir_free(void *data, size_t content_size) { ft_strdel(&redir->word); } - /* else */ - /* redir->type = 0; */ - /* redir->n = 0; */ - /* redir->close = 1; */ free(redir); } diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index 6cb3f743..be1d1d6c 100644 --- a/42sh/src/job-control/process_free.c +++ b/42sh/src/job-control/process_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/03/08 03:26:28 by wescande ### ########.fr */ +/* Updated: 2017/03/08 12:40:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ t_itof g_freemap[] = { {PROCESS_FUNCTION, process_free_subshell}, - {PROCESS_BUILTIN, NULL}, + {PROCESS_BUILTIN, process_free_cmd}, {PROCESS_FILE, process_free_cmd}, {PROCESS_SUBSHELL, process_free_subshell}, {PROCESS_WHILE, process_free_cond}, @@ -36,6 +36,6 @@ void process_free(void *content, size_t content_size) return ; if (g_freemap[p->type].f) (g_freemap[p->type].f)(p); - ft_lstdel(&p->redirs, ft_lst_cfree); + ft_lstdel(&p->redirs, redir_free); free(p); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index e20fcf4f..798ba6ce 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/03/08 12:20:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/08 12:41:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */