I forgot what this changes, read the diff

This commit is contained in:
pathetic 2025-05-13 21:31:04 +01:00
parent 9d759fcee7
commit c7d70b13f4
3 changed files with 63 additions and 47 deletions

View file

@ -7,6 +7,7 @@
global sys_read
global sys_wait4
global sys_exit
global sys_getcwd
global environ
extern main
@ -17,49 +18,53 @@ environ: dq 0
section .text
sys_open:
mov rax, 2 ; __NR_open
mov rax, 2
syscall
ret
sys_close:
mov rax, 3 ; __NR_close
mov rax, 3
syscall
ret
sys_getdents64:
mov rax, 217 ; __NR_getdents64
mov rax, 217
syscall
ret
sys_stat:
mov rax, 4 ; __NR_stat (legacy)
mov rax, 4
syscall
ret
sys_read:
mov rax, 0 ; __NR_read
mov rax, 0
syscall
ret
sys_write:
mov rax, 1 ; __NR_write
mov rax, 1
syscall
ret
sys_wait4:
mov rax, 61 ; __NR_wait4
mov rax, 61
syscall
ret
sys_exit:
mov rax, 60 ; __NR_exit
mov rax, 60
syscall
ret
sys_getcwd:
mov rax, 79
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
mov rdi, rax
mov rax, 60
syscall

View file

@ -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 */
#endif

View file

@ -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();
}
}