diff --git a/src/assembly/syscalls.asm b/src/assembly/syscalls.asm index ba704fb..48200b0 100644 --- a/src/assembly/syscalls.asm +++ b/src/assembly/syscalls.asm @@ -7,59 +7,64 @@ global sys_read global sys_wait4 global sys_exit + global sys_getcwd global environ extern main section .data -environ: dq 0 + environ: dq 0 section .text -sys_open: - mov rax, 2 ; __NR_open - syscall - ret + sys_open: + mov rax, 2 + syscall + ret -sys_close: - mov rax, 3 ; __NR_close - syscall - ret + sys_close: + mov rax, 3 + syscall + ret -sys_getdents64: - mov rax, 217 ; __NR_getdents64 - syscall - ret + sys_getdents64: + mov rax, 217 + syscall + ret -sys_stat: - mov rax, 4 ; __NR_stat (legacy) - syscall - ret + sys_stat: + mov rax, 4 + syscall + ret -sys_read: - mov rax, 0 ; __NR_read - syscall - ret + sys_read: + mov rax, 0 + syscall + ret -sys_write: - mov rax, 1 ; __NR_write - syscall - ret + sys_write: + mov rax, 1 + syscall + ret -sys_wait4: - mov rax, 61 ; __NR_wait4 - syscall - ret + sys_wait4: + mov rax, 61 + syscall + ret -sys_exit: - mov rax, 60 ; __NR_exit - syscall - ret + sys_exit: + mov rax, 60 + syscall + ret -_start: - ; SysV ABI: rdi=argc, rsi=argv, rdx=envp - mov [rel environ], rdx - call main - mov rdi, rax ; exit code = return value of main - mov rax, 60 ; __NR_exit - syscall \ No newline at end of file + sys_getcwd: + mov rax, 79 + syscall + ret + + _start: + mov [rel environ], rdx + call main + mov rdi, rax + mov rax, 60 + syscall diff --git a/src/headers/nstdo.h b/src/headers/nstdo.h index 499268d..351b6b3 100644 --- a/src/headers/nstdo.h +++ b/src/headers/nstdo.h @@ -1,5 +1,5 @@ -#ifndef MINI_STDIO_H -#define MINI_STDIO_H +#ifndef nstdo_h +#define nstdo_h #ifdef __cplusplus extern "C" { @@ -12,7 +12,6 @@ typedef long off_t; extern char **environ; char *getenv(const char *name); -/* minimal open flags */ #define O_RDONLY 0 #define O_WRONLY 1 #define O_RDWR 2 @@ -21,20 +20,17 @@ char *getenv(const char *name); #define O_APPEND 1024 #define O_CLOEXEC 02000000 -/* minimal stdarg */ typedef __builtin_va_list va_list; #define va_start(ap,last) __builtin_va_start(ap,last) #define va_arg(ap,type) __builtin_va_arg(ap,type) #define va_end(ap) __builtin_va_end(ap) -/* syscall numbers */ #define SYS_READ 0 #define SYS_WRITE 1 #define SYS_OPEN 2 #define SYS_CLOSE 3 #define SYS_LSEEK 8 -/* raw syscall wrapper */ static inline long _syscall3(long nr, long a1, long a2, long a3) { long ret; asm volatile( @@ -99,4 +95,4 @@ struct stat_t { } #endif -#endif /* MINI_STDIO_H */ \ No newline at end of file +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6f3dbb5..8e7ad59 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,7 @@ extern long sys_stat(const char *pathname, struct stat_t *statbuf); extern long sys_write(int fd, const void *buf, size_t count); extern long sys_read(int fd, void *buf, size_t count); extern void sys_exit(int status); +extern void sys_getcwd(char *buf, size_t size); static int streq(const char *a, const char *b) { while (*a && *b) { @@ -133,6 +134,18 @@ static void lsCommand(const char *path, int longFlag) { sys_close(fd); } +static void getcwdCommand() { + char buf[1024]; + sys_getcwd(buf, sizeof(buf)); + long len = str_len(buf); + if (len > 0 && buf[len - 1] != '/') { + buf[len++] = '/'; + } + buf[len] = '\0'; + buf[len++] = '\n'; + sys_write(1, buf, len); +} + int main(int argc, char *argv[], char *envp[]) { (void)argc; (void)argv; (void)envp; char buf[512]; @@ -184,6 +197,8 @@ int main(int argc, char *argv[], char *envp[]) { int longFlag = (opt && streq(opt, "-l")) ? 1 : 0; const char *path = arg ? arg : "."; lsCommand(path, longFlag); + } else if (streq(cmd, "cwd")) { + getcwdCommand(); } }