-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_in_path.c
123 lines (103 loc) · 2.65 KB
/
find_in_path.c
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
#include "shell.h"
int check_file(char *full_path);
/**
* find_program - find a program in path
* @data: a pointer to the program's data
* Return: 0 if success, errcode otherwise
*/
int find_program(data_of_program *data)
{
int i = 0, ret_code = 0;
char **directories;
if (!data->command_name)
return (2);
/**if is a full_path or an executable in the same path */
if (data->command_name[0] == '/' || data->command_name[0] == '.')
return (check_file(data->command_name));
free(data->tokens[0]);
data->tokens[0] = str_concat(str_duplicate("/"), data->command_name);
if (!data->tokens[0])
return (2);
directories = tokenize_path(data);/* search in the PATH */
if (!directories || !directories[0])
{
errno = 127;
return (127);
}
for (i = 0; directories[i]; i++)
{/* appends the function_name to path */
directories[i] = str_concat(directories[i], data->tokens[0]);
ret_code = check_file(directories[i]);
if (ret_code == 0 || ret_code == 126)
{/* the file was found, is not a directory and has execute permisions*/
errno = 0;
free(data->tokens[0]);
data->tokens[0] = str_duplicate(directories[i]);
free_array_of_pointers(directories);
return (ret_code);
}
}
free(data->tokens[0]);
data->tokens[0] = NULL;
free_array_of_pointers(directories);
return (ret_code);
}
/**
* tokenize_path - tokenize the path in directories
* @data: a pointer to the program's data
* Return: array of path directories
*/
char **tokenize_path(data_of_program *data)
{
int i = 0;
int counter_directories = 2;
char **tokens = NULL;
char *PATH;
/* get the PATH value*/
PATH = env_get_key("PATH", data);
if ((PATH == NULL) || PATH[0] == '\0')
{/*path not found*/
return (NULL);
}
PATH = str_duplicate(PATH);
/* find the number of directories in the PATH */
for (i = 0; PATH[i]; i++)
{
if (PATH[i] == ':')
counter_directories++;
}
/* reserve space for the array of pointers */
tokens = malloc(sizeof(char *) * counter_directories);
/*tokenize and duplicate each token of path*/
i = 0;
tokens[i] = str_duplicate(_strtok(PATH, ":"));
while (tokens[i++])
{
tokens[i] = str_duplicate(_strtok(NULL, ":"));
}
free(PATH);
PATH = NULL;
return (tokens);
}
/**
* check_file - checks if exists a file, if it is not a dairectory and
* if it has excecution permisions for permisions.
* @full_path: pointer to the full file name
* Return: 0 on success, or error code if it exists.
*/
int check_file(char *full_path)
{
struct stat sb;
if (stat(full_path, &sb) != -1)
{
if (S_ISDIR(sb.st_mode) || access(full_path, X_OK))
{
errno = 126;
return (126);
}
return (0);
}
/*if not exist the file*/
errno = 127;
return (127);
}