-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute_command.c
239 lines (214 loc) · 4.66 KB
/
execute_command.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "shell.h"
int search_for_command(char **tokens, char *line);
char *search_in_dir(char **tokens, char *line);
char *exe_in_dir(char **tokens, char *line);
char *remove_quotes(char *command);
/**
* execute_command - executes command line agruments and handles path
* @tokens: command and arguments
* @line: line read
* Return: 0 on success, else returns commandPath
*/
int execute_command(char **tokens, char *line)
{
int line_number = 0;
int status, childExitStatus;
char *commandPath;
pid_t pid;
if (tokens[0][0] == '\"' && tokens[0][myCustomStrlen(tokens[0]) - 1] == '\"')
{
tokens[0] = remove_quotes(tokens[0]);
}
if (search_for_command(tokens, line) == 0)
{
pid = fork();
if ((pid) == -1)
{
perror("Error: Failed to fork the current process.\n");
free(line);
freeTokens(tokens);
exit(EXIT_FAILURE);
}
if (pid == 0)
{
if ((execve(tokens[0], tokens, NULL) == -1))
{
if (myCustomStrchr(tokens[0], '/') != NULL)
{
free(line);
freeTokens(tokens);
exit(127);
}
commandPath = exe_in_dir(tokens, line);
free(commandPath);
}
}
else
{
wait(&status);
if (WIFEXITED(status))
{
childExitStatus = WEXITSTATUS(status);
return (childExitStatus);
}
}
}
else
{
line_number++;
errMessage(tokens, line_number);
free(line);
freeTokens(tokens);
exit(127);
}
return (0);
}
/**
* search_for_command - finds command in current directory
* @tokens: command and arguments
* @line: line read
* Return: 1 on error, 0 on success
*/
int search_for_command(char **tokens, char *line)
{
char *commandPath;
if (myCustomStrchr(tokens[0], '/') != NULL) /* Check if cmd has path*/
{
return (0);
}
else
{
commandPath = search_in_dir(tokens, line);
if (commandPath != NULL)
{
free(commandPath);
return (0);
}
else
{
free(commandPath);
return (1);
}
}
return (0);
}
/**
* search_in_dir - searches in directories for full path
* @tokens: command and arguments entered by user
* @line: line read
* Return: Null if not found, commandPath if found in dir
*/
char *search_in_dir(char **tokens, char *line)
{
char *path, *commandPath, *pathCopy, *token = NULL;
int line_number = 0;
path = myCustomGetenv("PATH");
pathCopy = myCustomStrdup(path);
if (pathCopy == NULL)
{
line_number++;
errMessage(tokens, line_number);
free(line);
freeTokens(tokens);
exit(127);
}
token = strtok(pathCopy, ":");
while (token != NULL)
{
commandPath = malloc(myCustomStrlen(token)
+ myCustomStrlen(tokens[0]) + 2);
if (commandPath == NULL)
{
line_number++;
errMessage(tokens, line_number);
free(pathCopy);
free(line);
freeTokens(tokens);
exit(127);
}
myCustomStrcpy(commandPath, token);
myCustomStrcat(commandPath, "/"); /* Append a slash to directory path */
myCustomStrcat(commandPath, tokens[0]); /* Append command name to dir path */
if (access(commandPath, X_OK) == 0)
{
free(pathCopy);
return (commandPath);
}
free(commandPath);
token = strtok(NULL, ":"); /* Move to next directory in PATH */
}
free(pathCopy);
return (NULL);
}
/**
* exe_in_dir - executes command found in path
* @tokens: command and argument
* @line: line read
* Return: Null on success, nothing on error
*/
char *exe_in_dir(char **tokens, char *line)
{
char *path, *commandPath, *pathCopy, *token = NULL;
int line_number = 0;
path = myCustomGetenv("PATH");
pathCopy = myCustomStrdup(path);
if (pathCopy == NULL)
{
perror("Error: Failed to allocate memory for pathCopy.\n");
free(line);
freeTokens(tokens);
exit(127);
}
token = strtok(pathCopy, ":");
while (token != NULL)
{
commandPath = malloc(myCustomStrlen(token)
+ myCustomStrlen(tokens[0]) + 2);
if (commandPath == NULL)
{
perror("Error: Failed to allocate memory for commandPath.\n");
free(pathCopy);
free(line);
freeTokens(tokens);
exit(127);
}
myCustomStrcpy(commandPath, token);
myCustomStrcat(commandPath, "/");
myCustomStrcat(commandPath, tokens[0]);
if (access(commandPath, X_OK) == 0)
{
if ((execve(commandPath, tokens, environ) == -1))
{
line_number++;
errMessage(tokens, line_number);
free(commandPath);
free(pathCopy);
free(line);
freeTokens(tokens);
exit(127);
}
}
free(commandPath);
token = strtok(NULL, ":");
}
free(pathCopy);
return (NULL);
}
/**
* remove_quotes - removes quotes so that a command can be executed
* @command: command
*
* Return: command without quotes
*/
char *remove_quotes(char *command)
{
int i, j;
int len = myCustomStrlen(command);
for (i = 0, j = 0; i < len; i++)
{
if (command[i] != '\"')
command[j++] = command[i];
}
command[j] = '\0';
return (command);
}