-
Notifications
You must be signed in to change notification settings - Fork 0
/
_check_path.c
47 lines (45 loc) · 1.15 KB
/
_check_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
#include "main.h"
/**
* _check_path - checks if a command exists in one of the directories
* in the PATH environment variable.
* @cmd: the command to check.
*
* Return: pointer to the full path of the command if found,
* otherwise NULL.
*/
char *_check_path(char *cmd)
{
char *path, **path_directories, *full_path;
unsigned int i;
list_t *head = NULL;
struct stat st;
/* Get value of the PATH environment variable */
path = _getenv("PATH");
if (path != NULL)
{
path_directories = _split_line(path, ":");
for (i = 0; path_directories[i] != NULL; i++)
_add_node_end(&head, path_directories[i]);
}
for (i = 0; _get_node_at_index(head, i) != NULL; i++)
{
full_path = malloc(sizeof(char) * (_strlen(cmd) +
_strlen(_get_node_at_index(head, i)->str + 2)));
if (full_path == NULL)
return (NULL);
full_path = _strcat(_get_node_at_index(head, i)->str, "/");
full_path = _strcat(full_path, &cmd[0]);
full_path = _strcat(full_path, "\0");
if (stat(full_path, &st) == 0)
{
if (access(full_path, F_OK) == 0)
{
free(path_directories);
return (full_path);
}
}
free(full_path);
}
free(path_directories);
return (NULL);
}