-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_path.c
More file actions
130 lines (121 loc) · 3.7 KB
/
command_path.c
File metadata and controls
130 lines (121 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* command_path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aouchaad <aouchaad@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/08 17:13:43 by aouchaad #+# #+# */
/* Updated: 2023/06/23 15:56:34 by aouchaad ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *command_starts_with_slash(char *command, \
t_variables *var, char ***paths)
{
struct stat file_state;
lstat(command, &file_state);
is_der_or_isnt_reg(command, file_state);
if (access(command, X_OK) == -1)
{
print_error(command, "Permission denied");
exit (126);
}
var->error = access(command, F_OK);
if (var->error == 0)
{
free_double_pointer((*paths));
return (command);
}
else
print_error(command, strerror(errno));
free_double_pointer((*paths));
if (ft_strcmp(strerror(errno), "Not a directory") == 0)
exit(126);
exit(127);
return (NULL);
}
char *join_command_whith_pointslash(char **command_path, \
t_variables *var, char *command, char ***paths)
{
(*command_path) = ft_sssstrjoin(ft_strdup("./"), ft_strdup(command));
var->error = access((*command_path), F_OK);
if (var->error == 0)
{
free_double_pointer((*paths));
return ((*command_path));
}
else
{
print_error(command, "No such file or directory");
free_double_pointer((*paths));
exit (127);
}
return (NULL);
}
char *unset_path_case(char **command_path, \
char *command, t_variables *var, char ***paths)
{
(*command_path) = ft_sssstrjoin(ft_strdup("./"), ft_strdup(command));
var->error = access((*command_path), F_OK);
if (var->error == 0)
{
free_double_pointer((*paths));
return ((*command_path));
}
else
{
print_error(command, "No such file or directory");
exit (127);
}
return (NULL);
}
int correct_path_normal_case(char **command_path, \
t_variables *var, char *command, char ***paths)
{
int i;
i = 0;
while ((*paths) != NULL && (*paths)[i] && command != NULL)
{
(*command_path) = ft_sssstrjoin(ft_strdup((*paths)[i]), ft_strdup("/"));
(*command_path) = ft_sssstrjoin((*command_path), ft_strdup(command));
var->error = access((*command_path), F_OK);
if (var->error == 0)
{
free_double_pointer((*paths));
return (1);
}
free_pointer(&(*paths)[i]);
(*paths)[i] = NULL;
free_pointer(&(*command_path));
i++;
}
return (0);
}
char *command_path(t_environment_path *envp, char *command)
{
char **paths;
char *command_path;
t_environment_path *tmp;
t_variables var;
tmp = envp;
var.path_exist = 0;
paths = NULL;
look_for_path(&tmp, &var, &paths);
if (command != NULL && check_caractere(command, '/') == 1)
return (command_starts_with_slash(command, &var, &paths));
if (tmp && tmp->variable != NULL && \
tmp->valeur != NULL && tmp->valeur[0] == '\0')
return (join_command_whith_pointslash(&command_path, \
&var, command, &paths));
if (var.path_exist == 0 && command != NULL)
return (unset_path_case(&command_path, command, &var, &paths));
if (correct_path_normal_case(&command_path, &var, command, &paths) == 1)
return (command_path);
if (command != NULL)
print_error(command, "command not found");
free(paths);
paths = NULL;
exit(127);
return (NULL);
}