diff --git a/srcs/minishell.h b/srcs/minishell.h index 30a7d9d..61ef3fb 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/03/05 19:05:34 by marde-vr ### ########.fr */ +/* Updated: 2024/03/06 10:19:27 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ # include # include "../libft/libft.h" # include "fcntl.h" +# include typedef enum e_token_type { @@ -115,5 +116,6 @@ int get_cmd_count(t_cmd *cmds); int get_args_count(t_cmd *cmds); char **get_cmd_args(t_msh *msh); void remove_command_from_msh(t_msh *msh); +int file_access(t_msh *msh, int *found); #endif diff --git a/srcs/path.c b/srcs/path.c index 115fade..f3b29ba 100644 --- a/srcs/path.c +++ b/srcs/path.c @@ -6,11 +6,12 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */ -/* Updated: 2024/03/06 08:42:49 by marde-vr ### ########.fr */ +/* Updated: 2024/03/06 10:18:53 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include char **split_paths_from_env(t_env *env) { @@ -30,7 +31,7 @@ char **split_paths_from_env(t_env *env) } if (!path_in_envp) { - ft_printf_fd(2, "msh: error: PATH not found\n"); + ft_printf_fd(2, "minishell: error: PATH not found\n"); return (0); } return (ft_split(cur_env_var->value, ':')); @@ -78,26 +79,32 @@ void free_paths(char **paths) free(paths); } -void get_cmd_path(t_msh *msh) +void get_path(t_msh *msh, int *found) { char **paths; + + paths = split_paths_from_env(msh->env); + if (!paths) + { + free_paths(paths); + ft_exit(msh, 1); + } + find_cmd_path(msh, paths, found); + free_paths(paths); +} + +void get_cmd_path(t_msh *msh) +{ int found; found = 0; - if (ft_strchr(msh->cmds->token, '/') - && access(msh->cmds->token, X_OK) != -1) - found = 1; - else + if (ft_strchr(msh->cmds->token, '/')) { - paths = split_paths_from_env(msh->env); - if (!paths) - { - free_paths(paths); - ft_exit(msh, 1); - } - find_cmd_path(msh, paths, &found); - free_paths(paths); + if (!file_access(msh, &found)) + return ; } + else + get_path(msh, &found); if (!found) { ft_printf_fd(2, "%s: command not found\n", msh->cmds->token); diff --git a/srcs/utils.c b/srcs/utils.c index ceca5cf..405fafd 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */ -/* Updated: 2024/03/05 18:19:32 by marde-vr ### ########.fr */ +/* Updated: 2024/03/06 10:19:58 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,3 +35,25 @@ void ft_exit(t_msh *msh, int exit_code) free_msh(msh); exit(exit_code); } + +int file_access(t_msh *msh, int *found) +{ + if (open(msh->cmds->token, O_DIRECTORY) != -1) + { + ft_printf_fd(2, "minishell: %s: Is a directory\n", msh->cmds->token); + g_return_code = 126; + return (0); + } + if (access(msh->cmds->token, X_OK) != -1) + *found = 1; + else + { + ft_printf_fd(2, "minishell: %s: ", msh->cmds->token); + perror(""); + g_return_code = 127; + if (access(msh->cmds->token, F_OK) != -1) + g_return_code = 126; + return (0); + } + return (1); +}