-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_location.c
65 lines (47 loc) · 2.29 KB
/
get_location.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "main.h"
char *get_location(char *command){
char *path, *path_copy, *path_token, *file_path;
int command_length, directory_length;
struct stat buffer;
path = getenv("PATH");
if (path){
/* Duplicate the path string -> remember to free up memory for this because strdup allocates memory that needs to be freed*/
path_copy = strdup(path);
/* Get length of the command that was passed */
command_length = strlen(command);
/* Let's break down the path variable and get all the directories available*/
path_token = strtok(path_copy, ":");
while(path_token != NULL){
/* Get the length of the directory*/
directory_length = strlen(path_token);
/* allocate memory for storing the command name together with the directory name */
file_path = malloc(command_length + directory_length + 2); /* NB: we added 2 for the slash and null character we will introduce in the full command */
/* to build the path for the command, let's copy the directory path and concatenate the command to it */
strcpy(file_path, path_token);
strcat(file_path, "/");
strcat(file_path, command);
strcat(file_path, "\0");
/* let's test if this file path actually exists and return it if it does, otherwise try the next directory */
if (stat(file_path, &buffer) == 0){
/* return value of 0 means success implying that the file_path is valid*/
/* free up allocated memory before returning your file_path */
free(path_copy);
return (file_path);
}
else{
/* free up the file_path memory so we can check for another path*/
free(file_path);
path_token = strtok(NULL, ":");
}
}
/* if we don't get any file_path that exists for the command, we return NULL but we need to free up memory for path_copy */
free(path_copy);
/* before we exit without luck, let's see if the command itself is a file_path that exists */
if (stat(command, &buffer) == 0)
{
return (command);
}
return (NULL);
}
return (NULL);
}