-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_the_PATH.c
49 lines (48 loc) · 892 Bytes
/
find_the_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
#include "shell.h"
/**
*fpath - find the path to call execve
*@input: the commands
*@PATH: the PATH
*@c: the string needed
*Return: string to call on execve
*/
char *fpath(char **input, char *PATH, char *c)
{
char *token = NULL, *fpathbuff = NULL, *concat = NULL;
static char tmp[256];
int PATHc = 0, pflag = 0, i = 0, len = 0;
struct stat x;
c = NULL;
c = _strdup(PATH);
PATHc = path_members(c);
token = strtok(c, ": =");
while (token != NULL)
{
concat = concatation(tmp, input, token);
if (stat(concat, &x) == 0)
{
fpathbuff = concat;
pflag = 1;
break;
}
if (i < PATHc - 2)
{
len = _strlen(token);
if (token[len + 1] == ':')
{
if (stat(input[0], &x) == 0)
{
fpathbuff = input[0];
pflag = 1;
break;
}
}
}
i++;
token = strtok(NULL, ":");
}
if (pflag == 0)
fpathbuff = input[0];
free(c);
return (fpathbuff);
}