This repository was archived by the owner on Aug 3, 2024. It is now read-only.
forked from achambers1136/simple-shell-cs210
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
116 lines (95 loc) · 3.44 KB
/
executor.c
File metadata and controls
116 lines (95 loc) · 3.44 KB
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
/* executor.c */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include "parser.h"
#include "history.h"
#include "alias.h"
int getpath() {
printf("%s", getenv("PATH"));
printf("\n\n");
return 0;
}
int setpath(int argc, char* argv[]) {
if (argc <= 1) {
fprintf(stderr, "Syntax error.\n\tUsage: setpath [path]\n\n");
return 1;
}
setenv("PATH", argv[1], 1);
printf("PATH set successfully!\n\n");
return 0;
}
int cd(int argc, char* argv[]) {
if (argc <= 1) {
chdir(getenv("HOME"));
return 0;
}
if (chdir(argv[1]) != 0) {
perror("ERROR: Current working directory was not changed: ");
return 1;
}
return 0;
}
int shell_exec_ext(int argc, char* argv[]) {
if (argc < 0) return 1;
if (argc == 0) return 0;
pid_t res = fork();
if (res < 0) {
fprintf(stderr, "Couldn't spawn a child process.\n");
return 1;
}
if (res == 0) {
char* PATH_spl;
char* rest = getenv("PATH"); // likely replace this with global path once implemented, doesn't contain current dir
int status = -1;
// Support for current dir, ideally should be included in path however
char lpath[512] = "./";
strcat(lpath, argv[0]);
status = execv(lpath, argv);
while ((PATH_spl = strtok_r(rest, ":", &rest)) && status == -1) {
char* path = strdup(PATH_spl);
strcat(strcat(path, "/"), argv[0]);
//printf("[trying %s]\n", path);
status = execv(path, argv);
}
//printf("Child process finished. (status %d)\n", status);
if (status != 0) {
fprintf(stderr, "'%s' is not recognised as a file or internal/external command: ", argv[0]);
perror("\0");
}
exit(status);
} else {
wait(NULL);
printf("\n");
}
return 0;
}
int shell_exec(int argc, char* argv[]) {
if (argc < 0) return 1; // err
if (argc == 0) return 0;
if (argc > 1) { if (strcmp(argv[0], "alias") == 0) return alias(argc, argv); } // special case, still need to add alias to history if no args
if (strcmp(argv[0], "unalias") == 0) return unalias(argc, argv);
argc = parseAliases(argc, argv); // replace aliases with their values
if (argc < 0) return 1;
/* While the command is a history invocation or alias then replace it with the
appropriate command from history or the aliased command respectively */
if (strcspn(argv[0], "!") == 0) {
argc = retrieveHistory(argv);
if (argc < 0) return 1;
argc = parseAliases(argc, argv); // in case new alias added since addition to history
} else {
addToHistory(argc, argv);
if (strcmp(argv[0], "history") == 0) return printHistory();
}
/* If command is built-in invoke appropriate function */
if (strcmp(argv[0], "exit") == 0) return 70;
else if (strcmp(argv[0], "getpath") == 0) return getpath();
else if (strcmp(argv[0], "setpath") == 0) return setpath(argc, argv);
else if (strcmp(argv[0], "cd") == 0) return cd(argc, argv);
else if (strcmp(argv[0], "alias") == 0) return alias(argc, argv);
else if (strcmp(argv[0], "unalias") == 0) return unalias(argc, argv);
/* Else execute command as an external process */
else return shell_exec_ext(argc, argv);
}