I forgot what this changes, read the diff
This commit is contained in:
parent
9d759fcee7
commit
c7d70b13f4
3 changed files with 63 additions and 47 deletions
|
|
@ -7,59 +7,64 @@
|
||||||
global sys_read
|
global sys_read
|
||||||
global sys_wait4
|
global sys_wait4
|
||||||
global sys_exit
|
global sys_exit
|
||||||
|
global sys_getcwd
|
||||||
global environ
|
global environ
|
||||||
|
|
||||||
extern main
|
extern main
|
||||||
|
|
||||||
section .data
|
section .data
|
||||||
environ: dq 0
|
environ: dq 0
|
||||||
|
|
||||||
section .text
|
section .text
|
||||||
|
|
||||||
sys_open:
|
sys_open:
|
||||||
mov rax, 2 ; __NR_open
|
mov rax, 2
|
||||||
syscall
|
syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
sys_close:
|
sys_close:
|
||||||
mov rax, 3 ; __NR_close
|
mov rax, 3
|
||||||
syscall
|
syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
sys_getdents64:
|
sys_getdents64:
|
||||||
mov rax, 217 ; __NR_getdents64
|
mov rax, 217
|
||||||
syscall
|
syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
sys_stat:
|
sys_stat:
|
||||||
mov rax, 4 ; __NR_stat (legacy)
|
mov rax, 4
|
||||||
syscall
|
syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
sys_read:
|
sys_read:
|
||||||
mov rax, 0 ; __NR_read
|
mov rax, 0
|
||||||
syscall
|
syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
sys_write:
|
sys_write:
|
||||||
mov rax, 1 ; __NR_write
|
mov rax, 1
|
||||||
syscall
|
syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
sys_wait4:
|
sys_wait4:
|
||||||
mov rax, 61 ; __NR_wait4
|
mov rax, 61
|
||||||
syscall
|
syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
sys_exit:
|
sys_exit:
|
||||||
mov rax, 60 ; __NR_exit
|
mov rax, 60
|
||||||
syscall
|
syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
_start:
|
sys_getcwd:
|
||||||
; SysV ABI: rdi=argc, rsi=argv, rdx=envp
|
mov rax, 79
|
||||||
mov [rel environ], rdx
|
syscall
|
||||||
call main
|
ret
|
||||||
mov rdi, rax ; exit code = return value of main
|
|
||||||
mov rax, 60 ; __NR_exit
|
_start:
|
||||||
syscall
|
mov [rel environ], rdx
|
||||||
|
call main
|
||||||
|
mov rdi, rax
|
||||||
|
mov rax, 60
|
||||||
|
syscall
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef MINI_STDIO_H
|
#ifndef nstdo_h
|
||||||
#define MINI_STDIO_H
|
#define nstdo_h
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
@ -12,7 +12,6 @@ typedef long off_t;
|
||||||
extern char **environ;
|
extern char **environ;
|
||||||
char *getenv(const char *name);
|
char *getenv(const char *name);
|
||||||
|
|
||||||
/* minimal open flags */
|
|
||||||
#define O_RDONLY 0
|
#define O_RDONLY 0
|
||||||
#define O_WRONLY 1
|
#define O_WRONLY 1
|
||||||
#define O_RDWR 2
|
#define O_RDWR 2
|
||||||
|
|
@ -21,20 +20,17 @@ char *getenv(const char *name);
|
||||||
#define O_APPEND 1024
|
#define O_APPEND 1024
|
||||||
#define O_CLOEXEC 02000000
|
#define O_CLOEXEC 02000000
|
||||||
|
|
||||||
/* minimal stdarg */
|
|
||||||
typedef __builtin_va_list va_list;
|
typedef __builtin_va_list va_list;
|
||||||
#define va_start(ap,last) __builtin_va_start(ap,last)
|
#define va_start(ap,last) __builtin_va_start(ap,last)
|
||||||
#define va_arg(ap,type) __builtin_va_arg(ap,type)
|
#define va_arg(ap,type) __builtin_va_arg(ap,type)
|
||||||
#define va_end(ap) __builtin_va_end(ap)
|
#define va_end(ap) __builtin_va_end(ap)
|
||||||
|
|
||||||
/* syscall numbers */
|
|
||||||
#define SYS_READ 0
|
#define SYS_READ 0
|
||||||
#define SYS_WRITE 1
|
#define SYS_WRITE 1
|
||||||
#define SYS_OPEN 2
|
#define SYS_OPEN 2
|
||||||
#define SYS_CLOSE 3
|
#define SYS_CLOSE 3
|
||||||
#define SYS_LSEEK 8
|
#define SYS_LSEEK 8
|
||||||
|
|
||||||
/* raw syscall wrapper */
|
|
||||||
static inline long _syscall3(long nr, long a1, long a2, long a3) {
|
static inline long _syscall3(long nr, long a1, long a2, long a3) {
|
||||||
long ret;
|
long ret;
|
||||||
asm volatile(
|
asm volatile(
|
||||||
|
|
@ -99,4 +95,4 @@ struct stat_t {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* MINI_STDIO_H */
|
#endif
|
||||||
15
src/main.c
15
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_write(int fd, const void *buf, size_t count);
|
||||||
extern long sys_read(int fd, 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_exit(int status);
|
||||||
|
extern void sys_getcwd(char *buf, size_t size);
|
||||||
|
|
||||||
static int streq(const char *a, const char *b) {
|
static int streq(const char *a, const char *b) {
|
||||||
while (*a && *b) {
|
while (*a && *b) {
|
||||||
|
|
@ -133,6 +134,18 @@ static void lsCommand(const char *path, int longFlag) {
|
||||||
sys_close(fd);
|
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[]) {
|
int main(int argc, char *argv[], char *envp[]) {
|
||||||
(void)argc; (void)argv; (void)envp;
|
(void)argc; (void)argv; (void)envp;
|
||||||
char buf[512];
|
char buf[512];
|
||||||
|
|
@ -184,6 +197,8 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||||
int longFlag = (opt && streq(opt, "-l")) ? 1 : 0;
|
int longFlag = (opt && streq(opt, "-l")) ? 1 : 0;
|
||||||
const char *path = arg ? arg : ".";
|
const char *path = arg ? arg : ".";
|
||||||
lsCommand(path, longFlag);
|
lsCommand(path, longFlag);
|
||||||
|
} else if (streq(cmd, "cwd")) {
|
||||||
|
getcwdCommand();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue