-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath_finder.c
49 lines (48 loc) · 838 Bytes
/
path_finder.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
#include "shell.h"
/**
* path_finder - check the code.
* @array: a double pointer
* Return: double pointer.
*/
void path_finder(char **array)
{
char *path = getenv("PATH"), *str, *con, *token;
str = malloc(sizeof(char) * (_strlen(path) + 1));
if (str == NULL)
{
perror("ERROR");
exit(1);
}
_strcpy(str, path);
token = strtok(str, ":");
while (token != NULL)
{
con = malloc(sizeof(char) * (_strlen(token) + _strlen(array[0]) + 2));
if (con == NULL)
{
perror("ERROR");
free(str);
exit(1);
}
_strcpy(con, token);
_strcat(con, "/");
_strcat(con, array[0]);
if (access(con, X_OK) == 0)
{
execute_line(con, array);
break;
}
else
{
free(con);
con = NULL;
token = strtok(NULL, ":");
continue;
}
free(con);
}
if (con == NULL)
perror(array[0]);
free(con);
free(str);
}