diff options
Diffstat (limited to 'src/get_cmd_path.c')
| -rw-r--r-- | src/get_cmd_path.c | 62 |
1 files changed, 53 insertions, 9 deletions
diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 8a27584..56d9876 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,28 +6,53 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2024/12/17 19:20:08 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 14:33:58 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "minishell.h" +#include <errno.h> +#include <sys/errno.h> +#include <sys/unistd.h> +#include <unistd.h> -static char *get_absolute_cmd_path(char *cmd, t_env *env); -static char *find_in_path(char *cmd, t_env *env); +static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code); +static char *find_in_path(char *cmd, t_env *env, int *return_code); char **get_split_path(t_env *env); -char *get_cmd_path(char *cmd, t_env *env) +char *get_cmd_path(char *cmd, t_env *env, int *return_code) { + char *result; + if (cmd[0] == '/') - return (ft_strdup(cmd)); + { + result = ft_strdup(cmd); + if (access(result, F_OK) == -1) + { + free(result); + errno = ENOENT; + perror(cmd); + *return_code = 127; + return (NULL); + } + else if (access(result, X_OK) == -1) + { + free(result); + errno = EACCES; + perror(cmd); + *return_code = 126; + return (NULL); + } + return (result); + } else if (ft_strchr(cmd, '/')) - return (get_absolute_cmd_path(cmd, env)); + return (get_absolute_cmd_path(cmd, env, return_code)); else - return (find_in_path(cmd, env)); + return (find_in_path(cmd, env, return_code)); } -static char *get_absolute_cmd_path(char *cmd, t_env *env) +static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code) { char *cur_dir; char *result; @@ -38,16 +63,32 @@ static char *get_absolute_cmd_path(char *cmd, t_env *env) result = ft_strjoin(cur_dir, cmd); free(cur_dir); if (!result) + { + *return_code = 127; + errno = ENOENT; + perror(cmd); return (NULL); + } + if (access(result, F_OK) == -1) + { + free(result); + errno = ENOENT; + *return_code = 127; + perror(cmd); + return (NULL); + } if (access(result, X_OK) == -1) { free(result); + errno = EACCES; + *return_code = 126; + perror(cmd); return (NULL); } return (result); } -static char *find_in_path(char *cmd, t_env *env) +static char *find_in_path(char *cmd, t_env *env, int *return_code) { char *cur_path; char *cmd_path; @@ -70,6 +111,9 @@ static char *find_in_path(char *cmd, t_env *env) return (cmd_path); path++; } + *return_code = 127; + printf("%s:", cmd); + ft_putstr_fd(" command not found", 2); return (NULL); } |
