-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell.c
More file actions
63 lines (60 loc) · 1.85 KB
/
shell.c
File metadata and controls
63 lines (60 loc) · 1.85 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
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
void panic(char reason[]) {
printf("PANIC: %s", reason);
return;
}
int main() {
FILE *fptr;
FILE *host;
DIR *folder;
bool shell = true;
while (shell = true) {
host = fopen("/etc/hostname", "r");
char hostname[15];
char location[30];
char input[10];
fgets(hostname, 15, host);
hostname[strcspn(hostname, "\n")] = 0;
printf("%s:", hostname);
fgets(input, 10, stdin);
input[strcspn(input, "\n")] = 0;
// why wont strcmp work?
if (strcmp(input, "help") == 0) {
printf("help: displays this \n print: prints something(only input print)\n create: creates files\n version: check the version\n ls: displays the files in current location\n exit: stops this\n");
}
else if (strcmp(input, "print") == 0) {
char print[30];
printf("What would you like to print?(btw character limit is 30):\n");
fgets(print, 30, stdin);
printf("%s\n", print);
} else if (strcmp(input, "exit") == 0) {
return 0;
} else if (strcmp(input, "create") == 0) {
printf("Please input the location of this file (make sure to include the file name and extension at the end)\n");
fgets(location, 30, stdin);
fptr = fopen(location, "w");
} else if (strcmp(input, "version") == 0) {
printf("Really Crap SHell ver 0.1\n");
} else if (strcmp(input, "ls") == 0) {
struct dirent *entry;
int files = 0;
char fname[40];
folder = opendir(".");
if(folder == NULL) {
panic("How are you in a directory that does not exist?");
}
while( (entry=readdir(folder)) ) {
files++;
printf("FILE %3d: %s\n", files, entry->d_name);
}
closedir(folder);
}
else {
printf("rcsh: '%s' command not found, maybe use help?\n");
}
}
}