-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_location.c
More file actions
48 lines (44 loc) · 905 Bytes
/
Copy pathget_location.c
File metadata and controls
48 lines (44 loc) · 905 Bytes
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"
/**
* getlocation - get the PATH for the command
* @command: command
*/
char *getlocation(char *command)
{
char *path, *path_cpy, *path_token, *path_name;
struct stat buffer;
int command_len, token_len;
path = getenv("PATH");
if (path)
{
path_cpy = _strdup(path);
command_len = _strlen(command);
path_token = strtok(path_cpy, ":");
while (path_token != NULL)
{
token_len = _strlen(path_token);
path_name = malloc(token_len + command_len + 2);
strcpy(path_name, path_token);
strcat(path_name, "/");
strcat(path_name, command);
strcat(path_name, "\0");
if ((stat(path_name, &buffer)) == 0)
{
free(path_cpy);
return (path_name);
}
else
{
free(path_name);
path_token = strtok(NULL, ":");
}
}
free(path_cpy);
if ((stat(command, &buffer)) == 0)
{
return (command);
}
return (NULL);
}
return (NULL);
}