-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.c
175 lines (169 loc) · 3.34 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "shell.h"
/**
* path_execute - executes a command in the path
* @command: full path to the command
* @vars: pointer to struct of variables
*
* Return: 0 on success, 1 on failure
*/
int path_execute(char *command, vars_t *vars)
{
pid_t child_pid;
if (access(command, X_OK) == 0)
{
child_pid = fork();
if (child_pid == -1)
print_error(vars, NULL);
if (child_pid == 0)
{
if (execve(command, vars->av, vars->env) == -1)
print_error(vars, NULL);
}
else
{
wait(&vars->status);
if (WIFEXITED(vars->status))
vars->status = WEXITSTATUS(vars->status);
else if (WIFSIGNALED(vars->status) && WTERMSIG(vars->status) == SIGINT)
vars->status = 130;
return (0);
}
vars->status = 127;
return (1);
}
else
{
print_error(vars, ": Permission denied\n");
vars->status = 126;
}
return (0);
}
/**
* find_path - finds the PATH variable
* @env: array of environment variables
*
* Return: pointer to the node that contains the PATH, or NULL on failure
*/
char *find_path(char **env)
{
char *path = "PATH=";
unsigned int i, j;
for (i = 0; env[i] != NULL; i++)
{
for (j = 0; j < 5; j++)
if (path[j] != env[i][j])
break;
if (j == 5)
break;
}
return (env[i]);
}
/**
* check_for_path - checks if the command is in the PATH
* @vars: variables
*
* Return: void
*/
void check_for_path(vars_t *vars)
{
char *path, *path_dup = NULL, *check = NULL;
unsigned int i = 0, r = 0;
char **path_tokens;
struct stat buf;
if (check_for_dir(vars->av[0]))
r = execute_cwd(vars);
else
{
path = find_path(vars->env);
if (path != NULL)
{
path_dup = _strdup(path + 5);
path_tokens = tokenize(path_dup, ":");
for (i = 0; path_tokens && path_tokens[i]; i++, free(check))
{
check = _strcat(path_tokens[i], vars->av[0]);
if (stat(check, &buf) == 0)
{
r = path_execute(check, vars);
free(check);
break;
}
}
free(path_dup);
if (path_tokens == NULL)
{
vars->status = 127;
new_exit(vars);
}
}
if (path == NULL || path_tokens[i] == NULL)
{
print_error(vars, ": not found\n");
vars->status = 127;
}
free(path_tokens);
}
if (r == 1)
new_exit(vars);
}
/**
* execute_cwd - executes the command in the current working directory
* @vars: pointer to struct of variables
*
* Return: 0 on success, 1 on failure
*/
int execute_cwd(vars_t *vars)
{
pid_t child_pid;
struct stat buf;
if (stat(vars->av[0], &buf) == 0)
{
if (access(vars->av[0], X_OK) == 0)
{
child_pid = fork();
if (child_pid == -1)
print_error(vars, NULL);
if (child_pid == 0)
{
if (execve(vars->av[0], vars->av, vars->env) == -1)
print_error(vars, NULL);
}
else
{
wait(&vars->status);
if (WIFEXITED(vars->status))
vars->status = WEXITSTATUS(vars->status);
else if (WIFSIGNALED(vars->status) && WTERMSIG(vars->status) == SIGINT)
vars->status = 130;
return (0);
}
vars->status = 127;
return (1);
}
else
{
print_error(vars, ": Permission denied\n");
vars->status = 126;
}
return (0);
}
print_error(vars, ": not found\n");
vars->status = 127;
return (0);
}
/**
* check_for_dir - checks if the command is a part of a path
* @str: command
*
* Return: 1 on success, 0 on failure
*/
int check_for_dir(char *str)
{
unsigned int i;
for (i = 0; str[i]; i++)
{
if (str[i] == '/')
return (1);
}
return (0);
}