-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.c
49 lines (44 loc) · 926 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
#include "main.h"
/**
* _path - function that prints environment PATH
* @env: environment
* @first: the first user inputted argument
* @input: the tokenized arguments
* @ex_st: the exit status
* Return: 0 if successful
*/
int _path(char *first, char **input, char **env, int *ex_st)
{
int i;
char *temp, *left, *right;
char *new = NULL, *envcopy = NULL;
for (i = 0; (env[i] != (void *) NULL); i++)
{
envcopy = _strdup(env[i]);
left = strtok(envcopy, "= \t");
temp = strtok(NULL, "= \t");
if (_strcmp(left, "PATH") == 0)
{
right = strtok(temp, ": \t");
while (right)
{
new = pathstr(right, first);
if (access(new, X_OK) == 0)
{
if (fork() == 0)
execve(new, input, NULL);
else
wait(NULL);
*ex_st = 0;
free(new);
free(envcopy);
return (0);
}
right = strtok(NULL, ": \t");
free(new);
}
}
free(envcopy);
}
return (2);
}