-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
443 lines (385 loc) · 6.34 KB
/
main.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "main.h"
int main(void);
char *lineptr = NULL;
/**
* main - Entrance to program
*
* Return: Exit code
*/
int main(void)
{
atexit(clean_up);
signal(SIGINT, sig_int_handler);
while (1)
{
envstruct *head = NULL;
/** init_env_list(head); */
char *cmd;
char *argv[100];
int i = 0;
label:
if (isatty(fileno(stdin)))
write(1, "$ ", 2);
_getline();
cmd = strtok(lineptr, " \t\n");
if (cmd == NULL)
{
free(lineptr);
goto label;
}
while (cmd != NULL)
{
argv[i] = cmd;
cmd = strtok(NULL, " \t\n");
i++;
}
argv[i] = NULL;
if (is_builtin_cmd(argv[0]) == 1)
{
exec_builtin_cmd(argv, head);
}
else if (is_builtin_cmd(argv[0]) == 0)
{
exec_executable_cmd(argv[0], argv, environ);
}
else
{
perror(" ");
if (lineptr != NULL)
free(lineptr);
exit(127);
}
if (lineptr != NULL)
free(lineptr);
}
return (0);
}
/**
* _getline - Get line conditions
*
* Return: Characters from stdin
*/
char *_getline(void)
{
size_t numbytes = 0, newnumbytes;
ssize_t linelen;
linelen = getline(&lineptr, &numbytes, stdin);
if (linelen == -1)
{
if (feof(stdin))
{
if (isatty(fileno(stdin)))
{
write(1, "\n", 1);
}
exit(0);
}
perror("");
exit(1);
}
newnumbytes = strcspn(lineptr, "\n");
if (lineptr[newnumbytes] == '\n')
lineptr[newnumbytes] = '\0';
return (lineptr);
}
/**
* is_builtin_cmd - checks for lists of built-in command
* @cmd: the command checked
*
* Return: 0 or 1
*/
int is_builtin_cmd(char *cmd)
{
int i = 0;
const char *builtins[5] = {"exit", "env", "setenv", "unsetenv", "cd"};
while (i < 5)
{
if (strstr(cmd, builtins[i]) == cmd)
{
return (1);
}
i++;
}
return (0);
}
/**
* exec_builtin_cmd - execute builtin commands
* @argv: argument vector - points to arguments entered
* @head: head of lists.
*
* Return: void
*/
void exec_builtin_cmd(char **argv, envstruct *head)
{
if (strstr(argv[0], "exit") == argv[0])
{
if (argv[1] != NULL)
exit_cmd(atoi(argv[1]));
else
exit_cmd(0);
}
if (strstr(argv[0], "env") == argv[0])
{
env_cmd();
}
if (strstr(argv[0], "setenv") == argv[0])
{
setenv_cmd(argv, head);
}
if (strstr(argv[0], "unsetenv") == argv[0])
{
unsetenv_cmd(argv, head);
}
if (strstr(argv[0], "cd") == argv[0])
{
if (cd_cmd(argv) == -1)
{
perror("");
}
}
}
/**
* exec_executable_cmd - execute the executable commands
* @cmd: command
* @argv: argument variable
* @envp: argument variable
*
* Return: void
*/
void exec_executable_cmd(char *cmd, char **argv, char **envp)
{
char *full_path;
pid_t child_pid;
int status;
full_path = check_cmd(cmd);
if (full_path == NULL)
{
return;
}
child_pid = fork();
if (child_pid == -1)
{
perror("");
exit(1);
}
else if (child_pid == 0)
{
execve_cmd(full_path, argv, envp);
}
else
{
wait(&status);
}
if ((full_path != NULL) && (full_path != cmd))
{
free(full_path);
full_path = NULL;
}
}
/**
* exit_cmd - exits/terminates the shell
* @exit_code: exit status code
*
* Return: void
*/
void exit_cmd(int exit_code)
{
exit(exit_code);
}
/**
* env_cmd - lists all environment variables
*
* Return: void
*/
void env_cmd(void)
{
int i = 0;
while (environ[i] != NULL)
{
printf("%s\n", environ[i]);
i++;
}
}
/**
* init_env_list - Intialise environment variables
* @head: head node
*/
void init_env_list(envstruct *head)
{
while (environ != NULL)
{
char *key = strtok(*environ, "="), *val = strtok(NULL, "=");
head = insert_end(head, key, val);
environ++;
}
}
/**
* setenv_cmd - sets environment variable
* @argv: argument vector
* @head: head node
*/
void setenv_cmd(char **argv, envstruct *head)
{
if (argv != NULL)
{
if ((*(argv + 1) != NULL) && (*(argv + 2) != NULL))
{
head = insert_end(head, *(argv + 1), *(argv + 2));
print_all(head);
}
else
perror("setenv_cmd insert Error");
}
}
/**
* unsetenv_cmd - remove environment variable set
* @argv: argument vector
* @head: head node
*/
void unsetenv_cmd(char **argv, envstruct *head)
{
if (argv != NULL)
{
if ((*(argv + 1) != NULL))
{
remove_value(&head, *(argv + 1));
print_all(head);
}
else
perror("setenv_cmd remove Error");
}
}
/**
* cd_cmd - change directory
* @argv: argument vector
*
* Return: void
*/
int cd_cmd(char **argv)
{
int stat;
stat = chdir(argv[1]);
if (stat == -1)
{
perror("");
return (-1);
}
return (0);
}
/**
* _strdup - duplicates string along their memory size
* @str: the string to be dublicated
*
* Return: string or NULL
*/
char *_strdup(char *str)
{
int str_len;
char *new_str;
str_len = strlen(str) + 1;
new_str = malloc(str_len * sizeof(char));
if (new_str == NULL)
{
return (NULL);
}
strcpy(new_str, str);
return (new_str);
}
/**
* check_cmd - check cmd whether inbuilt or executable
* @cmd: commands (tokens)
*
* Return: void
*/
char *check_cmd(char *cmd)
{
if (cmd[0] == '/' || cmd[0] == '.')
{
if (access(cmd, X_OK) == 0)
{
return (cmd);
}
else
{
perror("");
exit(1);
}
}
else
{
int dir_len, cmd_len;
char *dir;
char *path_dup;
char *path = getenv("PATH");
char *full_path = NULL;
if (path == NULL)
return (NULL);
path_dup = _strdup(path);
dir = strtok(path_dup, ":");
while (dir)
{
dir_len = strlen(dir);
cmd_len = strlen(cmd);
full_path = malloc((dir_len + cmd_len + 2) * sizeof(char));
if (full_path == NULL)
{
free(full_path);
free(path_dup);
return (NULL);
}
strcpy(full_path, dir);
strcat(full_path, "/");
strcat(full_path, cmd);
if (access(full_path, X_OK) == 0)
{
free(path_dup);
return (full_path);
}
free(full_path);
full_path = NULL;
dir = strtok(NULL, ":");
}
free(full_path);
free(path_dup);
path_dup = NULL;
}
return (NULL);
}
/**
* execve_cmd - execute the commands
* @cmd: command
* @argv: argument variable
* @envp: argument variable
*
* Return: void
*/
void execve_cmd(char *cmd, char **argv, char **envp)
{
if (execve(cmd, argv, envp) == -1)
{
perror("");
exit(1);
}
}
/**
* clean_up - frees the lineptr upon exit
*
* Return: void
*/
void clean_up(void)
{
if (lineptr != NULL)
free(lineptr);
}
/**
* sig_int_handler - handles the exit signal
* @signum: the signal passed
*
* Return: void
*/
void sig_int_handler(int signum __attribute__((unused)))
{
if (isatty(fileno(stdin)))
printf("\n");
exit(0);
}