-
Notifications
You must be signed in to change notification settings - Fork 1
/
_path.c
50 lines (43 loc) · 859 Bytes
/
_path.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
#include "shell.h"
/**
* _path - search its path os a command
* @param: command to search.
* @env: environment variable
* Return: nothing.
*/
int _path(char **param, char **env)
{
struct stat st;
char *path1 = malloc(512);
char *PATH;
char **PathParsed;
char **Directories;
int indNIL = 0;
int i = 0;
PATH = _GetEnv("PATH", env);
PathParsed = ParseCommand(PATH, "=");
Directories = ParseCommand(PathParsed[0], ":");
for (i = 0; Directories[i] != NULL; i++)
{
_strcpy(path1, Directories[i]);
_strcat(path1, "/");
_strcat(path1, param[0]);
removeSpaces(path1);
if (stat(path1, &st) == 0)
{
param[0] = _strdup(path1);
break;
}
if (stat(param[0], &st) == 0)
{
break;
}
_strcpy(path1, "");
}
if (Directories[i] == NULL)
indNIL = 1;
free(PathParsed);
free(Directories);
free(path1);
return (indNIL);
}