-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmini_shell.c
More file actions
95 lines (84 loc) · 2.11 KB
/
mini_shell.c
File metadata and controls
95 lines (84 loc) · 2.11 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
// Maximum command length
#define MAX_CMD_LEN 256
#define MAX_ARGS 16
char cwd[MAX_CMD_LEN];
void execute_command(char *cmd) {
char *args[MAX_ARGS];
char *token = strtok(cmd, " ");
int i = 0;
while (token != NULL && i < MAX_ARGS - 1) {
args[i] = token;
token = strtok(NULL, " ");
i++;
}
args[i] = NULL;
// Empty command
if (i == 0) {
return;
}
// Built-in command: exit
if (strcmp(args[0], "exit") == 0) {
printf("Exiting mini shell...\n");
exit(0);
}
// Built-in command: readsegs, will read CS, DS, SS
if (strcmp(args[0], "readsegs") == 0) {
unsigned int cs, ds, ss;
asm volatile("mov %%cs, %0" : "=r"(cs));
asm volatile("mov %%ds, %0" : "=r"(ds));
asm volatile("mov %%ss, %0" : "=r"(ss));
printf("CS = 0x%x, DS = 0x%x, SS = 0x%x\n", cs, ds, ss);
return;
}
// Built-in command: cd
if (strcmp(args[0], "cd") == 0) {
if (i < 2) {
printf("Usage: cd <directory>\n");
} else {
if (chdir(args[1]) < 0) {
perror("cd failed");
}
else {
getcwd(cwd, sizeof(cwd));
}
}
return;
}
// Fork a child process
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
return;
}
if (pid == 0) {
// Child process
execvp(args[0], args);
perror("exec failed");
exit(1);
} else {
// Parent process waits
int status;
waitpid(pid, &status, 0);
}
}
int main() {
char cmd[MAX_CMD_LEN];
getcwd(cwd, sizeof(cwd));
printf("Welcome to mini shell (Ring0 Usermode Test)\n");
while (1) {
printf("mini_shell:%s>", cwd);
if (fgets(cmd, MAX_CMD_LEN, stdin) == NULL) {
printf("\nExiting mini shell...\n");
break;
}
// Remove newline
cmd[strcspn(cmd, "\n")] = '\0';
execute_command(cmd);
}
return 0;
}