From 170d0b74fa725196bca0fa549520d0d8bfa07576 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 15 Sep 2020 13:38:22 +0200 Subject: Fixing most of the bugs in path --- src/builtin/pwd.c | 5 ++--- src/eval/cmd.c | 13 ++++++------- src/main.c | 4 ++-- src/path.c | 48 ++++++++++++++++++++++++++++++++++++------------ 4 files changed, 46 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/builtin/pwd.c b/src/builtin/pwd.c index ba4544a..9e04fe9 100644 --- a/src/builtin/pwd.c +++ b/src/builtin/pwd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:40 by charles #+# #+# */ -/* Updated: 2020/09/14 21:27:54 by charles ### ########.fr */ +/* Updated: 2020/09/15 12:09:32 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,8 +26,7 @@ int builtin_pwd(char **argv, t_env env) { - char buf[PATH_MAX]; - char *working_directory; + char buf[PATH_MAX + 1]; (void)argv; (void)env; diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 8e6e72a..04d66aa 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 21:32:19 by charles ### ########.fr */ +/* Updated: 2020/09/15 13:07:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,15 +55,14 @@ int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid) if (param.builtin == NULL) { - if (!path_search(env, argv[0], param.exec_path)) + if ((status = path_search(env, argv[0], param.exec_path, true)) != 0) { - /* printf("--------%s---\n", env_search(env, "PATH")); */ - errorf("%s: command not found\n", argv[0]); + /* errorf("%s: command not found\n", argv[0]); */ ft_split_destroy(argv); - return (127); - } - if ((status = exec_path_check(param.exec_path)) != 0) return (status); + } + /* if ((status = exec_path_check(param.exec_path)) != 0) */ + /* return (status); */ } param.argv = argv; diff --git a/src/main.c b/src/main.c index 5173a5d..608e44c 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/14 21:32:07 by charles ### ########.fr */ +/* Updated: 2020/09/15 13:31:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,7 +74,7 @@ int main(int argc, char **argv, char **envp) /* } */ char env_exec_path[PATH_MAX + 1]; - if (!path_search(env, "env", env_exec_path)) + if (path_search(env, "env", env_exec_path, false) != 0) { ft_strcpy(env_exec_path, "/sbin/env"); /* errorf("env: command not found\n"); */ diff --git a/src/path.c b/src/path.c index 442389b..4e912ba 100644 --- a/src/path.c +++ b/src/path.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/27 15:51:01 by cacharle #+# #+# */ -/* Updated: 2020/09/14 20:44:39 by charles ### ########.fr */ +/* Updated: 2020/09/15 13:36:34 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,31 @@ #include "minishell.h" +int path_check(char exec_path[PATH_MAX + 1], bool in_path) +{ + struct stat statbuf; + /* char cwd[PATH_MAX + 1]; */ + + 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 (S_ISLNK(statbuf.st_mode)) */ + /* return (errorf_ret(127, "%s: command not found\n", exec_path)); */ + if (!in_path && !(statbuf.st_mode & 0444)) + return (errorf_ret(126, "%s: %s\n", exec_path, strerror(EACCES))); + /* if (*exec_path != '/') */ + /* { */ + /* getcwd(cwd, PATH_MAX + 1); */ + /* ft_strcat(cwd, "/"); */ + /* ft_strcat(cwd, exec_path); */ + /* ft_strcpy(exec_path, cwd); */ + /* } */ + return (0); + + +} + static bool st_dir_search(char *dirname, char *exec_name, char exec_path[PATH_MAX + 1]) { DIR *dir; @@ -38,7 +63,7 @@ static bool st_dir_search(char *dirname, char *exec_name, char exec_path[PATH_M return (false); } -bool path_search(t_env env, char *exec_name, char exec_path[PATH_MAX + 1]) +int path_search(t_env env, char *exec_name, char exec_path[PATH_MAX + 1], bool print) { char *current_dir; char *collon; @@ -47,29 +72,28 @@ bool path_search(t_env env, char *exec_name, char exec_path[PATH_MAX + 1]) if (ft_strchr(exec_name, '/') != NULL) { ft_strcpy(exec_path, exec_name); - return (true); + return (path_check(exec_path, false)); } - /* printf("==========\n"); */ if ((current_dir = env_search(env, "PATH")) == NULL) return (st_dir_search(getcwd(cwd, PATH_MAX + 1), exec_name, exec_path)); - /* printf("%s\n", current_dir); */ while ((collon = ft_strchr(current_dir, ':')) != NULL) { *collon = '\0'; - /* printf("%s\n", current_dir); */ if (*current_dir == '\0') current_dir = getcwd(cwd, PATH_MAX + 1); if (st_dir_search(current_dir, exec_name, exec_path)) - return (true); + { + *collon = ':'; + return (path_check(exec_path, true)); + } *collon = ':'; current_dir = collon + 1; } - /* printf("> %s\n", current_dir); */ - if (*current_dir == '\0') current_dir = getcwd(cwd, PATH_MAX + 1); if (st_dir_search(current_dir, exec_name, exec_path)) - return (true); - /* printf("yo\n"); */ - return (false); + return (path_check(exec_path, true)); + if (print) + errorf("%s: command not found\n", exec_name); + return (127); } -- cgit