-
Notifications
You must be signed in to change notification settings - Fork 2
/
shell.c
executable file
·218 lines (184 loc) · 5.5 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
/* Generic C Headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vfs.h"
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
typedef struct arg_info_s {
int argc;
char **argv;
} arg_info_t;
static char *current_dir = "/";
static int is_mount = 0;
arg_info_t tokenize(char *input) {
int num_cmds = 2;
for (int i = 0; i < strlen(input); i++) {
if (input[i] == ' ') ++num_cmds;
}
arg_info_t arg_info;
char *token;
arg_info.argv = calloc(num_cmds+1, sizeof(char*));
for (arg_info.argc = 0; (token = strtok(NULL, " ")) != NULL; arg_info.argc++) {
arg_info.argv[arg_info.argc] = token;
}
arg_info.argv[num_cmds] = (char*)'\0';
return arg_info;
}
char * prepend_path(char *path) {
if (path == NULL) { return NULL; }
if (path[0] != '/') {
char *full_path = calloc(strlen(path) + strlen(current_dir) + 1, sizeof(char));
strcpy(full_path, current_dir);
strcat(full_path, path);
return full_path;
}
return path;
}
void mount(arg_info_t args) {
if (args.argc == 0) {
for (int i = 0; i < MOUNT_LIMIT; i++) {
if (mount_table[i] != NULL) {
printf("%s on %s type %s\n", mount_table[i]->device_name, mount_table[i]->path, "FAT32");
}
}
return;
} else if (args.argc < 2) {
printf("usage: mount device mount-point\n");
return;
}
char *device_name = args.argv[args.argc - 2];
char *path = args.argv[args.argc - 1];
mount_fs(device_name, path);
is_mount = 1;
}
void umount(arg_info_t args) {
if (args.argc != 1) {
printf("usage: umount mount-point\n");
return;
}
unmount_fs(args.argv[0]);
is_mount = 0;
}
void ls(arg_info_t args) {
if (args.argc != 0) {
printf("usage: ls\n");
return;
}
int dir = opendir("/");
dir_entry_t file;
while ((file = readdir(dir)).name != NULL) {
if (file.dir == 1) printf(KRED "%s\n" KNRM, file.name);
else printf("%s\n", file.name);
}
closedir(dir);
}
void touch(arg_info_t args) {
if (args.argc != 1) {
printf("usage: touch filename\n");
return;
}
int fp = filecreate(prepend_path(args.argv[0]));
fileclose(fp);
}
void cat(arg_info_t args) {
if (args.argc != 1) {
printf("usage: cat filename\n");
return;
}
int fp = fileopen(prepend_path(args.argv[0]), BEGIN);
if (fp == -1) { printf("cat: %s: No Such File or Directory\n", args.argv[0]); return; }
int nr = 0;
char buffer[512];
while ((nr = fileread(fp, buffer, 512)) > 0) {
printf("%s", buffer);
}
fileclose(fp);
}
void cd(arg_info_t args) {
if (args.argc != 1) {
printf("usage: cd directory\n");
return;
}
changedir(args.argv[0]);
}
void rm(arg_info_t args) {
if (args.argc != 1) {
printf("usage: rm file\n");
return;
}
//int fp = fileopen(prepend_path());
deletefile(args.argv[0]);
}
void echo(arg_info_t args) {
if (args.argc != 2) {
printf("usage: echo word file\n");
return;
}
int fp = fileopen(prepend_path(args.argv[1]), BEGIN);
if (fp == -1) { printf("Error\n"); }
filewrite(fp, args.argv[0], strlen(args.argv[0]));
fileclose(fp);
}
void echoa(arg_info_t args) {
if (args.argc != 2) {
printf("usage: echo word file\n");
return;
}
int fp = fileopen(prepend_path(args.argv[1]), APPEND);
if (fp == -1) { printf("Error\n"); }
filewrite(fp, args.argv[0], strlen(args.argv[0]));
fileclose(fp);
}
int main(int argc, char **argv) {
char *input;
/* Temporarily auto mount hello */
//mount_fs("hello", "/");
//mount_fs("/dev/sde1", "/");
while (1) {
printf("> ");
input = calloc(80, sizeof(char));
input = fgets(input, 80, stdin);
input[strlen(input)-1] = '\0'; // Strip New Line
char *cmd = strtok(input, " ");
if (cmd != NULL) {
if (strcmp(cmd, "exit") == 0) {
free(input);
break;
} else if(strcmp(cmd, "mount") == 0) {
mount(tokenize(input));
} else if(strcmp(cmd, "umount") == 0) {
umount(tokenize(input));
} else if (is_mount == 1) {
if (strcmp(cmd, "ls") == 0) {
ls(tokenize(input));
} else if (strcmp(cmd, "touch") == 0) {
touch(tokenize(input));
} else if (strcmp(cmd, "cat") == 0) {
cat(tokenize(input));
} else if (strcmp(cmd, "cd") == 0) {
cd(tokenize(input));
} else if (strcmp(cmd, "pwd") == 0) {
} else if (strcmp(cmd, "rm") == 0) {
rm(tokenize(input));
} else if (strcmp(cmd, "echo") == 0) {
echo(tokenize(input));
} else if (strcmp(cmd, "echoa") == 0) {
echoa(tokenize(input));
} else {
printf("%s: Command Not Found\n", input);
}
} else {
printf("No File Systems Mounted!\n");
}
}
free(input);
}
return EXIT_SUCCESS;
}