-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_location.c
53 lines (52 loc) · 1.12 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
#include "shell.h"
char *path_duplicate;
/**
* get_cmd_path - Get command full path
* @command: command argument
* Return: Return the full path on success
*/
char *get_cmd_path(char *command)
{
char *path = getenv("PATH"), *path_duplicate, *path_token, *file_path;
int command_len, directory_len;
struct stat buffer;
if (path)
{
path_duplicate = strdup(path);
path_token = strtok(path_duplicate, ":");
while (path_token != NULL)
{
directory_len = strlen(path_token);
command_len = strlen(command);
file_path = malloc(directory_len + command_len + 2);
if (!file_path)
{
free(path_duplicate);
return (NULL);
}
strcpy(file_path, path_token);
strcat(file_path, "/");
strcat(file_path, command);
if (stat(file_path, &buffer) == 0)
{
free(path_duplicate);
return (file_path);
}
else
{
free(file_path);
file_path = NULL;
path_token = strtok(NULL, ":");
}
}
free(path_duplicate);
}
if (stat(command, &buffer) == 0)
{
file_path = malloc(strlen(command) + 1);
if (!file_path)
return (NULL);
strcpy(file_path, command);
}
return (file_path);
}