fixed file execution error handling

This commit is contained in:
mdev9
2024-03-06 10:21:23 +01:00
parent 4df96de4a3
commit d9b819165e
3 changed files with 48 additions and 17 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <sys/wait.h>
# include "../libft/libft.h"
# include "fcntl.h"
# include <sys/stat.h>
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

View File

@ -6,11 +6,12 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <unistd.h>
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);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}