-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_search.c
More file actions
46 lines (39 loc) · 1.07 KB
/
path_search.c
File metadata and controls
46 lines (39 loc) · 1.07 KB
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
#include "main.h"
/**
* Function: get_path
* -------------------
* Searches through the PATH environment variable to find the executable path of a command.
*
* command: The command whose path is to be found.
*
* Returns: The full path of the command if found, otherwise returns the command itself.
*/
char *get_path(char *command)
{
char *path = getenv("PATH");
char *path_copy = strdup(path);
char command_path[1024];
char *dir = strtok(path_copy, ":");
/* Loop through each directory in the PATH */
while (dir != NULL)
{
strcpy(command_path, dir);
/* Append '/' if it's not already present */
if (command_path[strlen(command_path) - 1] != '/')
{
strcat(command_path, "/");
}
/* Append the command to the path */
strcat(command_path, command);
/* Check if the command exists in the directory */
if (access(command_path, X_OK) == 0)
{
free(path_copy);
return (strdup(command_path));
}
/* Move to the next directory */
dir = strtok(NULL, ":");
}
free(path_copy);
return (command);
}