-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_exec.c
executable file
·205 lines (189 loc) · 3.36 KB
/
cmd_exec.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
200
201
202
203
204
205
#include "main.h"
/**
* is_cdir - checks ":" if is in the current directory.
* @path: type char pointer char.
* @i: type int pointer of index.
* Return: 1 if the path is searchable in the cd, 0 otherwise.
*/
int is_cdir(char *path, int *i)
{
if (path[*i] == ':')
return (1);
while (path[*i] != ':' && path[*i])
{
*i += 1;
}
if (path[*i])
*i += 1;
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_cdir(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_executable - determines if is an executable
*
* @datash: data structure
* Return: 0 if is not an executable, other number if it does
*/
int is_executable(data_shell *datash)
{
struct stat st;
int i;
char *input;
input = datash->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);
}
get_error(datash, 127);
return (-1);
}
/**
* check_error_cmd - verifies if user has permissions to access
*
* @dir: destination directory
* @datash: data structure
* Return: 1 if there is an error, 0 if not
*/
int check_error_cmd(char *dir, data_shell *datash)
{
if (dir == NULL)
{
get_error(datash, 127);
return (1);
}
if (_strcmp(datash->args[0], dir) != 0)
{
if (access(dir, X_OK) == -1)
{
get_error(datash, 126);
free(dir);
return (1);
}
free(dir);
}
else
{
if (access(datash->args[0], X_OK) == -1)
{
get_error(datash, 126);
return (1);
}
}
return (0);
}
/**
* cmd_exec - executes command lines
*
* @datash: data relevant (args and input)
* Return: 1 on success.
*/
int cmd_exec(data_shell *datash)
{
pid_t pd;
pid_t wpd;
int state;
int exec;
char *dir;
(void) wpd;
exec = is_executable(datash);
if (exec == -1)
return (1);
if (exec == 0)
{
dir = _which(datash->args[0], datash->_environ);
if (check_error_cmd(dir, datash) == 1)
return (1);
}
pd = fork();
if (pd == 0)
{
if (exec == 0)
dir = _which(datash->args[0], datash->_environ);
else
dir = datash->args[0];
execve(dir + exec, datash->args, datash->_environ);
}
else if (pd < 0)
{
perror(datash->av[0]);
return (1);
}
else
{
do {
wpd = waitpid(pd, &state, WUNTRACED);
} while (!WIFEXITED(state) && !WIFSIGNALED(state));
}
datash->status = state / 256;
return (1);
}