-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
280 lines (228 loc) · 5.54 KB
/
shell.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
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
typedef struct Function Function;
typedef struct BST BST;
struct Function
{
char *name;
void (*pfunction)(char**);
Function *left;
Function *right;
};
struct BST
{
Function *root;
};
BST *gen_bst();
//Handles signals like interruptions etc.
void signalhandler(int sig);
//Change Directory
void cd(char *args[]);
// If you do cd ../.. It appends them to prompt instead of removing files
//Subfunction of cd for reversal
char *cd_back(void);
//Lists items inside directory
void dir(char *args[]);
//To do: Add option of flags to do long list or recursive
// Handles execution of scripts or C files etc.
void execute_file(char *args[], int flag);
//Gets prompt
char *get_prompt(void);
//Lists enviroment variables
extern char **environ;
//bst bollocks
void insert(Function *root, Function *new);
int gen_hashvalue(char *fname);
Function *new_function(char *name, void (*pfunction)(char**));
int run(BST *tree, char *args[]);
int search_and_execute(Function *root, char *fName, char *args[]);
void execute_file(char *args[], int flag) {
pid_t pid = fork();
if (pid < 0) {
printf("Your fork failed you lose you're nothing\n");
}
if (pid == 0){
execvp(args[0], &args[1]);
}
else {
if (flag) {
wait(NULL);
}
kill(pid, SIGKILL);
}
}
char *get_prompt(void)
{
char *user = getenv("USER");
char *indicator = ">";
char *path = malloc(100);
getcwd(path, 100);
char *prompt = malloc(sizeof(char) * strlen(user) + strlen(path) + strlen(indicator) + 2);
sprintf(prompt, "%s ~/%s\n%s", user, strstr(path, user) + strlen(user) + 1, indicator);
return prompt;
}
void cd(char *args[])
{
char *dir;
if (args[1])
{
dir = (char*)malloc(strlen(getenv("PWD")) + strlen(args[1]) + 2);
if (!strcmp(args[1], ".."))
{
dir = cd_back();
}
else if (!strcmp(args[1], "."))
{
sprintf(dir, "%s", getenv("PWD"));
}
else
{
sprintf(dir, "%s/%s", getenv("PWD"), args[1]);
}
}
else
{
dir = getenv("HOME");
}
if (chdir(dir) || setenv("PWD", dir, 1)){
printf("You failed you lose you're nothing!\n");
}
}
char *cd_back() {
char *copy = strdup(getenv("PWD"));
char *finalPos = strrchr(copy, '/');
if (finalPos != copy) { //Ensure not root directory
*finalPos = '\0';
} else {
*(finalPos + 1) = '\0';
}
return copy;
}
void dir(char *args[]) {
if (args[1]) {
char *directory_name = (char *)malloc(sizeof(char) * strlen(getenv("PWD")) + 10);
if (directory_name == NULL) {
printf("malloc failed in dir function\n");
}
sprintf(directory_name, "./%s", args[1]);
DIR* pdir = opendir(directory_name);
free(directory_name);
if (dir == NULL) {
printf("You failed youre a loser youre nothing");
}
struct dirent *entity;
entity = readdir(pdir);
while (entity != NULL) {
printf("%s\n", entity->d_name);
entity =readdir(pdir);
}
closedir(pdir);
}
else {
DIR *pdir = opendir(".");
struct dirent *entity;
entity = readdir(pdir);
while (entity != NULL){
printf("%s\n", entity->d_name);
entity = readdir(pdir);
/*
Recursive???
if (entity->d_type == DT_DIR) {
;
}
*/
}
}
}
//Following commands take no arguments but take char** so they can go
//go into the tree.
void quit(char *args[])
{
exit(EXIT_SUCCESS);
}
void clr(char *args[])
{
printf("\033[2J");
}
void environf(char *args[])
{
for (char **env = environ; *env != NULL; env++) printf("%s\n", *env);
}
void signalhandler(int sig)
{
if (sig == 2) printf("Noooo come backkk dont sigint mee\n");
exit(1);
}
BST *gen_bst(void) {
BST *tree = (BST *)malloc(sizeof(BST));
tree->root = (Function*)malloc(sizeof(Function));
void(*shell_functions[])(char**) = {&cd, &dir, &quit, &clr, &environf};
char *string_functions[] = {"cd", "dir", "quit", "clr", "environ"};
tree->root = new_function(string_functions[0], shell_functions[0]);
int i = 1;
while (i < sizeof(string_functions)/ sizeof(string_functions[0])) {
insert(tree->root, new_function(string_functions[i], shell_functions[i]));
i++;
}
return tree;
}
Function *new_function(char *fname, void (*pfunction)(char *argv[])) {
Function *new = (Function *)malloc(sizeof(Function));
new->name = fname;
new->pfunction = pfunction;
return new;
}
void insert(Function *root, Function *new) {
if (new == NULL) {
printf("NEW FUNCTION IS EQUAL TO NULL YOU FAIL YOU LOSE\n");
}
int compare = strcmp(new->name, root->name);
if (compare < 0) {
if (root->left != NULL) {
insert(root->left, new);
}
else {
root->left = new;
}
}
if (compare >= 0) {
if (root->right != NULL) {
insert(root->right, new);
}
else {
root->right = new;
}
}
}
int run(BST *tree, char *args[]) {
return search_and_execute(tree->root, args[0], args);
}
int search_and_execute(Function *root, char *fName, char *args[]) {
int compare;
if (root != NULL)
{
compare= strcmp(fName, root->name);
} else {
return 0;
}
if (!compare) {
root->pfunction(args);
return 1;
}
else if ((root->left == NULL) && (root->right == NULL)) {
return 0;
}
else if (compare >= 1) {
return search_and_execute(root->right, fName, args);
}
else if (compare < 0) {
return search_and_execute(root->left, fName, args);
}
printf("search_and_execute failed");
}