Correctly structured project & implemented required functions
This commit is contained in:
parent
50c928f6f6
commit
c8cd18d21c
10 changed files with 513 additions and 76 deletions
52
src/main.c
Normal file
52
src/main.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include "headers/nstdo.h"
|
||||
extern long sys_read(int fd, void *buf, size_t cnt);
|
||||
extern long sys_write(int fd, const void *buf, size_t cnt);
|
||||
extern long sys_fork(void);
|
||||
extern long sys_execve(const char *filename, char *const argv[], char *const envp[]);
|
||||
extern long sys_wait4(int pid, int *wstatus, int options, void *rusage);
|
||||
extern void sys_exit(int status);
|
||||
|
||||
static int streq(const char *a, const char *b) {
|
||||
while (*a && *b) {
|
||||
if (*a != *b) return 0;
|
||||
a++; b++;
|
||||
}
|
||||
return *a == *b;
|
||||
}
|
||||
|
||||
static long str_len(const char *s) {
|
||||
long len = 0;
|
||||
while (s[len]) len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char *envp[]) {
|
||||
(void)argc; (void)argv;
|
||||
const char prompt[] = "$ ";
|
||||
char buf[512];
|
||||
while (1) {
|
||||
sys_write(1, prompt, sizeof(prompt)-1);
|
||||
long n = sys_read(0, buf, sizeof(buf)-1);
|
||||
if (n <= 0) break;
|
||||
buf[n-1] = '\0';
|
||||
if (streq(buf, "help")) {
|
||||
const char *h1 = " help - display this help message\n";
|
||||
const char *h2 = " exit - exit the shell\n";
|
||||
sys_write(1, h1, str_len(h1));
|
||||
sys_write(1, h2, str_len(h2));
|
||||
continue;
|
||||
}
|
||||
if (streq(buf, "exit")) {
|
||||
break;
|
||||
}
|
||||
char *args[] = { buf, NULL };
|
||||
long pid = sys_fork();
|
||||
if (pid == 0) {
|
||||
sys_execve(buf, args, envp);
|
||||
sys_exit(1);
|
||||
} else {
|
||||
sys_wait4(pid, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue