From 828dd435725ea315abd2ea9875325ee3b17041a9 Mon Sep 17 00:00:00 2001 From: "Nathan P. Reiner" Date: Thu, 15 Dec 2022 18:20:33 +0100 Subject: did this while lecture (builtins, parsing, exec and env by stdlib) --- lib/cstr/cstr.c | 4 ++-- lib/sys/brk.h | 3 ++- lib/sys/close.h | 2 +- lib/sys/dup2.h | 2 +- lib/sys/execve.h | 2 +- lib/sys/exit.h | 2 +- lib/sys/fork.h | 3 ++- lib/sys/mmap.h | 3 ++- lib/sys/munmap.h | 3 ++- lib/sys/pipe.h | 2 +- lib/sys/read.h | 3 ++- lib/sys/reboot.h | 3 ++- lib/sys/sbrk.h | 3 ++- lib/sys/start.S | 6 +++++- lib/sys/stat.h | 3 ++- lib/sys/write.h | 3 ++- 16 files changed, 30 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/cstr/cstr.c b/lib/cstr/cstr.c index fcb91f4..f464cf3 100644 --- a/lib/cstr/cstr.c +++ b/lib/cstr/cstr.c @@ -264,7 +264,7 @@ u8 previous_utf8(const char **c) void u64_to_cstr(u64 n, char *cstr, u64 length) { char *p = cstr + length - 1; - u64 d; + u64 d = 0; int i = 0; for (;n && i < length; ++i) { @@ -272,7 +272,7 @@ void u64_to_cstr(u64 n, char *cstr, u64 length) n /= 10; } - d = p - cstr; + d = p - cstr + 1; for (i = 0; i < length - d; ++i) cstr[i] = cstr[i + d]; diff --git a/lib/sys/brk.h b/lib/sys/brk.h index d2221a2..72f1edd 100644 --- a/lib/sys/brk.h +++ b/lib/sys/brk.h @@ -3,7 +3,8 @@ #include "syscalls.h" -static unsigned long brk(void * addr) { +static unsigned long brk(void * addr) +{ return syscall(BRK, addr); } diff --git a/lib/sys/close.h b/lib/sys/close.h index 2aa3a71..5a6fbe4 100644 --- a/lib/sys/close.h +++ b/lib/sys/close.h @@ -3,7 +3,7 @@ #include "syscalls.h" -int close(unsigned int fd) +static int close(unsigned int fd) { syscall(CLOSE, fd); } diff --git a/lib/sys/dup2.h b/lib/sys/dup2.h index 7726efb..9e67eec 100644 --- a/lib/sys/dup2.h +++ b/lib/sys/dup2.h @@ -3,7 +3,7 @@ #include "syscalls.h" -int dup2(unsigned int oldfd, unsigned int newfd) +static int dup2(unsigned int oldfd, unsigned int newfd) { return syscall(DUP2, oldfd, newfd); } diff --git a/lib/sys/execve.h b/lib/sys/execve.h index 04d6dea..62ebefc 100644 --- a/lib/sys/execve.h +++ b/lib/sys/execve.h @@ -3,7 +3,7 @@ #include "syscalls.h" -int execve(const char *pathname, char *const argv[], char *const envp[]) +static int execve(const char *pathname, char *const argv[], char *const envp[]) { return syscall(EXECVE, pathname, argv, envp); } diff --git a/lib/sys/exit.h b/lib/sys/exit.h index 516d714..0063a13 100644 --- a/lib/sys/exit.h +++ b/lib/sys/exit.h @@ -3,7 +3,7 @@ #include "syscalls.h" -_Noreturn void exit(int error_code) +static void exit(int error_code) { syscall(EXIT, error_code); } diff --git a/lib/sys/fork.h b/lib/sys/fork.h index 2ee3ae8..2a8adbf 100644 --- a/lib/sys/fork.h +++ b/lib/sys/fork.h @@ -3,7 +3,8 @@ #include "syscalls.h" -int fork() { +static int fork() +{ return syscall(FORK); } diff --git a/lib/sys/mmap.h b/lib/sys/mmap.h index 7e885c4..d555e68 100644 --- a/lib/sys/mmap.h +++ b/lib/sys/mmap.h @@ -10,7 +10,8 @@ #define MAP_SHARED 0x1 #define MAP_PRIVATE 0x2 -static void * mmap(void * addr, unsigned long size, int prot, int flags, int fd, int offset) { +static void * mmap(void * addr, unsigned long size, int prot, int flags, int fd, int offset) +{ return syscall(MMAP, size, prot, flags, fd, offset); } diff --git a/lib/sys/munmap.h b/lib/sys/munmap.h index 4a876d6..1d30fa2 100644 --- a/lib/sys/munmap.h +++ b/lib/sys/munmap.h @@ -2,7 +2,8 @@ #define MUNMAP_H #include "syscall.h" -static int munmap(void * addr, unsigned long size) { +static int munmap(void * addr, unsigned long size) +{ return syscall(MUNMAP, addr, size); } diff --git a/lib/sys/pipe.h b/lib/sys/pipe.h index f95f366..029d6bb 100644 --- a/lib/sys/pipe.h +++ b/lib/sys/pipe.h @@ -6,7 +6,7 @@ #define PIPE_IN 1 #define PIPE_OUT 0 -int pipe(int *filedes) +static int pipe(int *filedes) { return syscall(PIPE, filedes); } diff --git a/lib/sys/read.h b/lib/sys/read.h index 7dba6e7..61e5576 100644 --- a/lib/sys/read.h +++ b/lib/sys/read.h @@ -2,7 +2,8 @@ #define READ_H #include "syscalls.h" -static int read(unsigned int fd, char * buf, unsigned long count) { +static int read(unsigned int fd, char * buf, unsigned long count) +{ return syscall(READ, fd, buf, count, 0, 0); } diff --git a/lib/sys/reboot.h b/lib/sys/reboot.h index fd119c5..22893ca 100644 --- a/lib/sys/reboot.h +++ b/lib/sys/reboot.h @@ -11,7 +11,8 @@ #define REBOOT_SUSPEND 0xd000fce2 #define REBOOT_NEW_KERNEL 0x45584543 -static int reboot(int cmd) { +static int reboot(int cmd) +{ const int magic1 = 0xfee1dead; const int magic2 = 537993216; diff --git a/lib/sys/sbrk.h b/lib/sys/sbrk.h index 25d9c6c..159c644 100644 --- a/lib/sys/sbrk.h +++ b/lib/sys/sbrk.h @@ -3,7 +3,8 @@ #include "syscalls.h" -static int brk(void * addr) { +static int brk(void * addr) +{ return syscall(BRK, addr); } diff --git a/lib/sys/start.S b/lib/sys/start.S index deafd67..1faf691 100644 --- a/lib/sys/start.S +++ b/lib/sys/start.S @@ -1,6 +1,10 @@ .global _start _start: + mov (%rsp), %rdi + xor %rax, %rax + lea 8(%rsp, %rax, 8), %rsi + lea 16(%rsp, %rdi, 8), %rdx call main - mov %rax, %rdi + mov $0, %rdi mov $60, %rax syscall diff --git a/lib/sys/stat.h b/lib/sys/stat.h index 02bdc48..a6aa9a8 100644 --- a/lib/sys/stat.h +++ b/lib/sys/stat.h @@ -55,7 +55,8 @@ struct stat { u64 __[3]; }; -static int stat(const char * filename, struct stat * statbuf) { +static int stat(const char * filename, struct stat * statbuf) +{ return syscall(STAT, filename, statbuf); } diff --git a/lib/sys/write.h b/lib/sys/write.h index 59f7789..6ad748e 100644 --- a/lib/sys/write.h +++ b/lib/sys/write.h @@ -3,7 +3,8 @@ #include "syscalls.h" -static int write(unsigned int fd, const char * buf, unsigned long count) { +static int write(unsigned int fd, const char * buf, unsigned long count) +{ return syscall(WRITE, fd, buf, count); } -- cgit v1.2.3-70-g09d2