-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_handler.c
More file actions
70 lines (62 loc) · 1.21 KB
/
env_handler.c
File metadata and controls
70 lines (62 loc) · 1.21 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "shell.h"
/**
* my_path - function that print envment PATH
* @env: envment variable
* @xfirst: first splite into tokns with inptted args
* @inpt: input is split into tokens
* @exit_st: exit status
* Return: 0
*/
int my_path(char *xfirst, char **inpt, char **env, int *exit_st)
{
int i;
char *temp, *p_left, *p_right;
char *new = NULL, *env_cp = NULL;
for (i = 0; env[i] != NULL; i++)
{
env_cp = str_dup(env[i]);
p_left = strtok(env_cp, "= \t");
temp = strtok(NULL, "= \t");
if (str_cmp(p_left, "PATH") == 0)
{
p_right = strtok(temp, ": \t");
while (p_right)
{
new = path_str(p_right, xfirst);
if (access(new, X_OK) == 0)
{
if (fork() == 0)
execve(new, inpt, NULL);
else
wait(NULL);
*exit_st = 0;
free(new);
free(env_cp);
return (0);
}
p_right = strtok(NULL, ": \t");
free(new);
}
}
free(env_cp);
}
return (2);
}
/**
* print_env - print env variable
* @env: env variable to print
* @exit_st: exit status
* Return: 0 is succes
*/
int print_env(char **env, int *exit_st)
{
unsigned int i = 0;
while (env[i] != NULL)
{
printstr_(env[i]);
put_char('\n');
i++;
}
*exit_st = 0;
return (EXIT_SUCCESS);
}