-
Notifications
You must be signed in to change notification settings - Fork 0
/
shs_execve.c
199 lines (183 loc) · 3.39 KB
/
shs_execve.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "shs.h"
/**
* is_current_dir - checks if ":" is in the current directory path
* @path: pointer to the path string
* @i: pointer to the index
* Return: 1 if the path contains ":", 0 otherwise
*/
int is_current_dir(char *path, int *i)
{
if (path[*i] == ':')
return (1);
while (path[*i] != ':' && path[*i])
(*i)++;
if (path[*i])
(*i)++;
return (0);
}
/**
* _which - locates a command
* @cmd: command name
* @_environ: environment variable
* Return: location of the command
*/
char *_which(char *cmd, char **_environ)
{
char *path, *ptr_path, *token_path, *dir;
int len_dir, len_cmd, i;
struct stat st;
path = _getenv("PATH", _environ);
if (path)
{
ptr_path = _strdup(path);
len_cmd = _strlen(cmd);
token_path = _strtok(ptr_path, ":");
i = 0;
while (token_path != NULL)
{
if (is_current_dir(path, &i))
if (stat(cmd, &st) == 0)
return (cmd);
len_dir = _strlen(token_path);
dir = malloc(len_dir + len_cmd + 2);
_strcpy(dir, token_path);
_strcat(dir, "/");
_strcat(dir, cmd);
_strcat(dir, "\0");
if (stat(dir, &st) == 0)
{
free(ptr_path);
return (dir);
}
free(dir);
token_path = _strtok(NULL, ":");
}
free(ptr_path);
if (stat(cmd, &st) == 0)
return (cmd);
return (NULL);
}
if (cmd[0] == '/')
if (stat(cmd, &st) == 0)
return (cmd);
return (NULL);
}
/**
* is_execve - determines if a command is executable
* @shdata: data structure
* Return: 0 if the command is not executable, otherwise the index of '/'
*/
int is_execve(shell_data *shdata)
{
struct stat st;
int i;
char *input;
input = shdata->args[0];
for (i = 0; input[i]; i++)
{
if (input[i] == '.')
{
if (input[i + 1] == '.')
return (0);
if (input[i + 1] == '/')
continue;
else
break;
}
else if (input[i] == '/' && i != 0)
{
if (input[i + 1] == '.')
continue;
i++;
break;
}
else
break;
}
if (i == 0)
return (0);
if (stat(input + i, &st) == 0)
{
return (i);
}
error_code(shdata, 127);
return (-1);
}
/**
* check_cmd_error - verifies if the user has permission to access a command
* @dir: destination directory
* @shdata: data structure
* Return: 1 if there is an error, 0 if not
*/
int check_cmd_error(char *dir, shell_data *shdata)
{
if (!dir)
{
error_code(shdata, 127);
return (1);
}
if (_strcmp(shdata->args[0], dir) != 0)
{
if (access(dir, X_OK) == -1)
{
error_code(shdata, 126);
free(dir);
return (1);
}
free(dir);
}
else
{
if (access(shdata->args[0], X_OK) == -1)
{
error_code(shdata, 126);
return (1);
}
}
return (0);
}
/**
* cmd_execve - executes command lines
* @shdata: data structure containing the arguments and input
* Return: 1 on success
*/
int cmd_execve(shell_data *shdata)
{
pid_t pd;
pid_t wpd;
int state;
int exec;
char *dir;
(void) wpd;
exec = is_execve(shdata);
if (exec == -1)
return (1);
if (exec == 0)
{
dir = _which(shdata->args[0], shdata->_environ);
if (check_cmd_error(dir, shdata) == 1)
return (1);
}
pd = fork();
if (pd == 0)
{
if (exec == 0)
dir = _which(shdata->args[0], shdata->_environ);
else
dir = shdata->args[0];
execve(dir + exec, shdata->args, shdata->_environ);
}
else if (pd < 0)
{
perror(shdata->av[0]);
return (1);
}
else
{
do {
wpd = waitpid(pd, &state, WUNTRACED);
} while (!WIFEXITED(state) && !WIFSIGNALED(state));
}
shdata->status = state / 256;
return (1);
}