Skip to content

Commit d9b8191

Browse files
committed
fixed file execution error handling
1 parent 4df96de commit d9b8191

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

srcs/minishell.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
9-
/* Updated: 2024/03/05 19:05:34 by marde-vr ### ########.fr */
9+
/* Updated: 2024/03/06 10:19:27 by marde-vr ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,6 +20,7 @@
2020
# include <sys/wait.h>
2121
# include "../libft/libft.h"
2222
# include "fcntl.h"
23+
# include <sys/stat.h>
2324

2425
typedef enum e_token_type
2526
{
@@ -115,5 +116,6 @@ int get_cmd_count(t_cmd *cmds);
115116
int get_args_count(t_cmd *cmds);
116117
char **get_cmd_args(t_msh *msh);
117118
void remove_command_from_msh(t_msh *msh);
119+
int file_access(t_msh *msh, int *found);
118120

119121
#endif

srcs/path.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
9-
/* Updated: 2024/03/06 08:42:49 by marde-vr ### ########.fr */
9+
/* Updated: 2024/03/06 10:18:53 by marde-vr ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "minishell.h"
14+
#include <unistd.h>
1415

1516
char **split_paths_from_env(t_env *env)
1617
{
@@ -30,7 +31,7 @@ char **split_paths_from_env(t_env *env)
3031
}
3132
if (!path_in_envp)
3233
{
33-
ft_printf_fd(2, "msh: error: PATH not found\n");
34+
ft_printf_fd(2, "minishell: error: PATH not found\n");
3435
return (0);
3536
}
3637
return (ft_split(cur_env_var->value, ':'));
@@ -78,26 +79,32 @@ void free_paths(char **paths)
7879
free(paths);
7980
}
8081

81-
void get_cmd_path(t_msh *msh)
82+
void get_path(t_msh *msh, int *found)
8283
{
8384
char **paths;
85+
86+
paths = split_paths_from_env(msh->env);
87+
if (!paths)
88+
{
89+
free_paths(paths);
90+
ft_exit(msh, 1);
91+
}
92+
find_cmd_path(msh, paths, found);
93+
free_paths(paths);
94+
}
95+
96+
void get_cmd_path(t_msh *msh)
97+
{
8498
int found;
8599

86100
found = 0;
87-
if (ft_strchr(msh->cmds->token, '/')
88-
&& access(msh->cmds->token, X_OK) != -1)
89-
found = 1;
90-
else
101+
if (ft_strchr(msh->cmds->token, '/'))
91102
{
92-
paths = split_paths_from_env(msh->env);
93-
if (!paths)
94-
{
95-
free_paths(paths);
96-
ft_exit(msh, 1);
97-
}
98-
find_cmd_path(msh, paths, &found);
99-
free_paths(paths);
103+
if (!file_access(msh, &found))
104+
return ;
100105
}
106+
else
107+
get_path(msh, &found);
101108
if (!found)
102109
{
103110
ft_printf_fd(2, "%s: command not found\n", msh->cmds->token);

srcs/utils.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
9-
/* Updated: 2024/03/05 18:19:32 by marde-vr ### ########.fr */
9+
/* Updated: 2024/03/06 10:19:58 by marde-vr ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -35,3 +35,25 @@ void ft_exit(t_msh *msh, int exit_code)
3535
free_msh(msh);
3636
exit(exit_code);
3737
}
38+
39+
int file_access(t_msh *msh, int *found)
40+
{
41+
if (open(msh->cmds->token, O_DIRECTORY) != -1)
42+
{
43+
ft_printf_fd(2, "minishell: %s: Is a directory\n", msh->cmds->token);
44+
g_return_code = 126;
45+
return (0);
46+
}
47+
if (access(msh->cmds->token, X_OK) != -1)
48+
*found = 1;
49+
else
50+
{
51+
ft_printf_fd(2, "minishell: %s: ", msh->cmds->token);
52+
perror("");
53+
g_return_code = 127;
54+
if (access(msh->cmds->token, F_OK) != -1)
55+
g_return_code = 126;
56+
return (0);
57+
}
58+
return (1);
59+
}

0 commit comments

Comments
 (0)