-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_path.c
38 lines (32 loc) · 891 Bytes
/
search_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
#include "main.h"
/**
* searchPath - searches the directories in the PATH enviroment variables for
* the directory containing the binary file associated with a command
* @commandName: the name of the command whose binary is to be searched for
*
* Return: returns a pointer to the directory containing the binary file or
* NULL if the file is not found.
*/
char *searchPath(char *commandName)
{
/* search the path directories for the command */
/* declare variables */
list_t *head;
char *cmdDirPath;
/* initialize variables */
head = NULL;
cmdDirPath = NULL;
/* search for command file in PATH directories */
head = createPathDirList();
if (head == NULL)
return (NULL);
cmdDirPath = locateCmdDirPath(head, commandName);
if (cmdDirPath == NULL)
{
free_list(head);
return (NULL);
}
/* free the PATH directory list */
free_list(head);
return (cmdDirPath);
}