From 3693acaff9fbf3f34dc2907e95dd221d5a8bc9e4 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 14 Sep 2020 17:22:21 +0200 Subject: Refactoring exec_search_path to distinguish between command not found and malloc error --- src/eval/cmd.c | 26 +++++++------------------- src/eval/eval.c | 5 ++--- src/eval/exec.c | 52 +++++++++++++++++++++------------------------------- 3 files changed, 30 insertions(+), 53 deletions(-) (limited to 'src/eval') diff --git a/src/eval/cmd.c b/src/eval/cmd.c index ff2c904..455ff77 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/14 15:50:15 by charles ### ########.fr */ +/* Updated: 2020/09/14 17:18:05 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,19 +36,6 @@ int wrapped_cmd(void *void_param) } } -int check_exec_path(char *exec_path) -{ - struct stat statbuf; - - if (stat(exec_path, &statbuf) == -1) - return (errorf_ret(126, "%s: %s\n", exec_path, strerror(errno))); - if (S_ISDIR(statbuf.st_mode)) - return (errorf_ret(126, "%s: Is a directory\n", exec_path)); - if (!(statbuf.st_mode & 0444)) - return (errorf_ret(126, "%s: %s\n", exec_path, strerror(EACCES))); - return (0); -} - int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) { t_fork_param_cmd param; @@ -67,14 +54,15 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) if (param.builtin == NULL) { - param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]); - if (param.exec_path == NULL) + status = exec_search_path(path, env_search(env, "PATH"), argv[0], ¶m.exec_path); + if (status != 0) { - errorf("%s: command not found\n", argv[0]); + if (status == 127) + errorf("%s: command not found\n", argv[0]); ft_split_destroy(argv); - return (127); + return (status); } - if ((status = check_exec_path(param.exec_path)) != 0) + if ((status = exec_path_check(param.exec_path)) != 0) return (status); } diff --git a/src/eval/eval.c b/src/eval/eval.c index d35d491..351c870 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/13 20:38:06 by charles #+# #+# */ -/* Updated: 2020/09/13 20:45:20 by charles ### ########.fr */ +/* Updated: 2020/09/14 16:49:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ int fork_wrap( if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) || (fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1)) exit(EXIT_FAILURE); - if ((status = wrapped(passed)) == EVAL_FATAL) // FIXME detect fatal in child (pipe ?) + if ((status = wrapped(passed)) == EVAL_FATAL) exit(EXIT_FAILURE); exit(status); } @@ -52,7 +52,6 @@ int fork_wrap( { waitpid(*child_pid, child_pid, 0); close(fds[FD_WRITE]); - /* close(fds[FD_READ]); */ return (WEXITSTATUS(*child_pid)); } return (0); diff --git a/src/eval/exec.c b/src/eval/exec.c index fb97bdc..e8e13e1 100644 --- a/src/eval/exec.c +++ b/src/eval/exec.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:06:11 by charles #+# #+# */ -/* Updated: 2020/07/18 09:39:53 by charles ### ########.fr */ +/* Updated: 2020/09/14 17:20:50 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,35 +17,24 @@ #include "eval.h" -/* -** \brief Check if executable name is already a path -** \param exec_name Executable name -** \return True if valid -*/ - -/* bool exec_is_path(char *exec_name) */ -/* { */ -/* return (ft_strncmp(exec_name, "../", 3) == 0 */ -/* || ft_strncmp(exec_name, "./", 2) == 0 */ -/* || ft_strncmp(exec_name, "/", 1) == 0); */ -/* } */ - /* ** \brief Check if executable path is valid ** \param exec_path Executable path -** \return True if valid +** \return status code */ -bool exec_is_valid(char *exec_path) +int exec_path_check(char *exec_path) { struct stat statbuf; - if (stat(exec_path, &statbuf) != 0) - return (false); - if (!S_ISREG(statbuf.st_mode)) // also need to manage link - return (false); - // could test permission but probably handled by execve - return (true); + if (stat(exec_path, &statbuf) == -1) + return (errorf_ret(127, "%s: %s\n", exec_path, strerror(errno))); + if (S_ISDIR(statbuf.st_mode)) + return (errorf_ret(126, "%s: Is a directory\n", exec_path)); + // + if (!(statbuf.st_mode & 0444)) + return (errorf_ret(126, "%s: %s\n", exec_path, strerror(EACCES))); + return (0); } /* @@ -56,19 +45,20 @@ bool exec_is_valid(char *exec_path) ** \return Executable path or NULL if not found or path update error */ -char *exec_search_path(t_path path, char *path_var, char *exec_name) +int exec_search_path(t_path path, char *path_var, char *exec_name, char **exec_path) { - char *exec_path; - if (ft_strchr(exec_name, '/') != NULL) // TODO test recursive link - return (exec_name); + { + *exec_path = exec_name; + return (0); + } // TODO if PATH contain empty path, consider current directory files as cmd - if ((exec_path = ft_htget(path, exec_name)) == NULL) + if ((*exec_path = ft_htget(path, exec_name)) == NULL) { if (path_update(path, path_var) == NULL) // optimise by not updating not changed path in ht - return (NULL); // FIXME need to distiguish between malloc error and cmd not found error - if ((exec_path = ft_htget(path, exec_name)) == NULL) - return (NULL); + return (EVAL_FATAL); // FIXME need to distiguish between malloc error and cmd not found error + if ((*exec_path = ft_htget(path, exec_name)) == NULL) + return (127); } - return (exec_path); + return (0); } -- cgit