68 lines
1.2 KiB
C
68 lines
1.2 KiB
C
#include "syscall.h"
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
const char* shell_prompt = "rocksh> ";
|
|
|
|
char buf[256];
|
|
size_t buf_offset = 0;
|
|
|
|
char path[256];
|
|
|
|
const char* bin_dir = "/bin/";
|
|
|
|
void parse_cmd() {
|
|
if (strcmp(buf, "exit") == 0) {
|
|
printf("bye!\n");
|
|
exit(0);
|
|
}
|
|
|
|
if (strcmp(buf, "motd") == 0) {
|
|
printf("rockos is the best!\n");
|
|
return;
|
|
}
|
|
|
|
strcpy(path, bin_dir);
|
|
strcat(path, buf);
|
|
|
|
int pid = fork();
|
|
if (pid == 0) {
|
|
if (!exec(path)) {
|
|
printf("failed to execute %s\n", path);
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
int status;
|
|
waitpid(pid, &status);
|
|
printf("process exited with status %d\n", status);
|
|
}
|
|
|
|
void reset() {
|
|
memset(buf, 0, 256);
|
|
buf_offset = 0;
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
(void)argc;
|
|
(void)argv;
|
|
|
|
printf(shell_prompt);
|
|
while (1) {
|
|
char c;
|
|
int rd = read(0, &c, 1);
|
|
if (rd > 0) {
|
|
if (c == '\n') {
|
|
parse_cmd();
|
|
reset();
|
|
printf(shell_prompt);
|
|
continue;
|
|
}
|
|
buf[buf_offset++] = c;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
} |