-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.c
More file actions
47 lines (44 loc) · 737 Bytes
/
Copy pathpath.c
File metadata and controls
47 lines (44 loc) · 737 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 "shell.h"
/**
* _getpath - get path
* @cmd: command
* Return: full command
*/
char *_getpath(char *cmd)
{
char *env, *full_cmd, *dir;
int i;
struct stat rr;
for (i = 0; cmd[i]; i++)
{
if (cmd[i] == '/')
{
if (stat(cmd, &rr) == 0)
return (_strdup(cmd));
return (NULL);
}
}
env = _getenv("PATH");
if (env == NULL)
return (NULL);
dir = strtok(env, ":");
while (dir != NULL)
{
full_cmd = malloc(_strlen(dir) + _strlen(cmd) + 2);
if (full_cmd)
{
_strcpy(full_cmd, dir);
_strcat(full_cmd, "/");
_strcat(full_cmd, cmd);
if (stat(full_cmd, &rr) == 0)
{
free(env);
return (full_cmd);
}
free(full_cmd);
dir = strtok(NULL, ":");
}
}
free(env);
return (NULL);
}