-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_for_exec.c
More file actions
101 lines (92 loc) · 1.74 KB
/
search_for_exec.c
File metadata and controls
101 lines (92 loc) · 1.74 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
#include "minishell.h"
static char *clean_return(char **path)
{
clean_2d_arr(path);
return (NULL);
}
static char *custom_strjoin(char const *s1, char const *s2)
{
char *res;
size_t i;
if (!s1 || !s2)
return (NULL);
res = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 2));
if (!res)
return (NULL);
i = 0;
while (*s1 != '\0')
{
res[i] = *(s1++);
i++;
}
res[i] = '/';
i++;
while (*s2 != '\0')
{
res[i] = *(s2++);
i++;
}
res[i] = '\0';
return (res);
}
static char *path_loop_clean(t_data *d, DIR *dir, char **path, int i)
{
char *val;
val = custom_strjoin(path[i], d->program_name);
clean_2d_arr(path);
closedir(dir);
if (!val)
return (NULL);
d->was_allocation = 1;
return (val);
}
static char *path_loop(t_data *d, char **path, char *program_name, int i)
{
DIR *dir;
struct dirent *ent;
d->program_name = program_name;
while (path[++i])
{
dir = opendir(path[i]);
if (!dir && errno != EACCES && errno != ENOENT && errno != ENOTDIR)
return (clean_return(path));
else if (dir)
{
ent = readdir(dir);
while (ent != NULL)
{
if (!ft_strcmp(ent->d_name, program_name))
return (path_loop_clean(d, dir, path, i));
ent = readdir(dir);
}
if (closedir(dir) < 0)
return (clean_return(path));
}
}
clean_2d_arr(path);
return (program_name);
}
char *search_for_exec(t_data *d, char *program_name)
{
char *val;
char **path;
if (get_env_val(d, "PATH=", &val))
return (NULL);
if (!val)
{
val = ft_strjoin("./", program_name);
if (!val)
return (NULL);
d->was_allocation = 1;
return (val);
}
path = ft_split(val, ':');
if (val)
free(val);
if (!path)
return (NULL);
val = path_loop(d, path, program_name, -1);
if (!val)
return (NULL);
return (val);
}