-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearchpathfile.c
More file actions
51 lines (40 loc) 路 961 Bytes
/
Copy pathsearchpathfile.c
File metadata and controls
51 lines (40 loc) 路 961 Bytes
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
#include "simple_shell.h"
/**
* getpathfile - Searches in an environment variable for a progr file path,
* stores subdirs in an array, and validate if the prog file path is available
* @args: user input array arguments
* Return: 0 on success, -1 on failure
*/
int getpathfile(char **args)
{
int index = 0;
char **directories = malloc(sizeof(char *) * BUFFSIZE);
char *path, *path_var;
path = _strdup(_getenv("PATH"));
if (path == NULL || directories == NULL)
{
free_function(1, path);
return (-1);
}
path_var = strtok(path, "=:");
while (path_var != NULL)
{
directories[index] = _strdup(path_var);
if (directories[index] == NULL)
{
free_function(1, path);
free_function(2, directories);
return (-1);
}
path_var = strtok(NULL, "=:");
index++;
}
directories[index] = path_var;
if (searchdir(directories, args) == -1)
{
free_function(1, path);
return (-1);
}
free_function(1, path);
return (0);
}