diff options
author | dzwdz | 2022-07-26 20:40:29 +0200 |
---|---|---|
committer | dzwdz | 2022-07-26 20:44:29 +0200 |
commit | 599c916d4cdd06765e0869b0a4d685820384f500 (patch) | |
tree | 7355c35189a4b6e92249f59c76e6d89d5b432f29 /src | |
parent | 350124fb4cfefc90c8f4a60de3da3c5d7da44f01 (diff) |
user: posix-compatible FILE* opening
Diffstat (limited to 'src')
-rw-r--r-- | src/user/app/init/main.c | 10 | ||||
-rw-r--r-- | src/user/app/init/shell.c | 10 | ||||
-rw-r--r-- | src/user/lib/elfload.c | 2 | ||||
-rw-r--r-- | src/user/lib/elfload.h | 2 | ||||
-rw-r--r-- | src/user/lib/include/bits/file.h | 4 | ||||
-rw-r--r-- | src/user/lib/include/errno.h | 3 | ||||
-rw-r--r-- | src/user/lib/include/stdio.h | 14 | ||||
-rw-r--r-- | src/user/lib/malloc.c | 2 | ||||
-rw-r--r-- | src/user/lib/stdlib.c | 52 |
9 files changed, 59 insertions, 40 deletions
diff --git a/src/user/app/init/main.c b/src/user/app/init/main.c index a898844..71220af 100644 --- a/src/user/app/init/main.c +++ b/src/user/app/init/main.c @@ -15,7 +15,7 @@ __attribute__((section(".text.startup"))) int main(void) { elf_selfreloc(); - file_reopen(stdout, "/kdev/com1", 0); + freopen("/kdev/com1", "a+", stdout); printf("in init (stage 2), loaded at 0x%x\n", &_image_base); MOUNT("/tmp/", tmpfs_drv()); @@ -42,11 +42,11 @@ int main(void) { } if (!fork()) { - if (!file_reopen(stdout, "/kdev/com1", 0)) { + if (!freopen("/kdev/com1", "a+", stdout)) { printf("couldn't open /kdev/com1\n"); // TODO borked _syscall_exit(1); } - if (!file_reopen(stdin, "/kdev/com1", 0)) { + if (!freopen("/kdev/com1", "r", stdin)) { printf("couldn't open /kdev/com1\n"); _syscall_exit(1); } @@ -57,11 +57,11 @@ int main(void) { } if (!fork()) { - if (!file_reopen(stdout, "/vga_tty", 0)) { + if (!freopen("/vga_tty", "a+", stdout)) { printf("couldn't open /vga_tty\n"); // TODO borked _syscall_exit(1); } - if (!file_reopen(stdin, "/keyboard", 0)) { + if (!freopen("/keyboard", "r", stdin)) { printf("couldn't open /keyboard\n"); _syscall_exit(1); } diff --git a/src/user/app/init/shell.c b/src/user/app/init/shell.c index 0c8b9b4..795bb5e 100644 --- a/src/user/app/init/shell.c +++ b/src/user/app/init/shell.c @@ -45,7 +45,7 @@ static int readline(char *buf, size_t max) { } static void cmd_cat_ls(const char *args, bool ls) { - libc_file *file; + FILE *file; static char buf[512]; int len; // first used for strlen(args), then length of buffer @@ -60,9 +60,9 @@ static void cmd_cat_ls(const char *args, bool ls) { } } - file = file_open(buf, 0); + file = fopen(buf, "r"); } else if (ls) { /* ls default argument */ - file = file_open("/", 0); + file = fopen("/", "r"); } else { /* cat default argument */ file = file_clone(stdin); } @@ -153,7 +153,7 @@ void shell_loop(void) { } if (!fork()) { - if (redir && !file_reopen(stdout, redir, OPEN_CREATE)) { + if (redir && !freopen(redir, "w", stdout)) { // TODO stderr _syscall_exit(0); } @@ -161,7 +161,7 @@ void shell_loop(void) { if (!strcmp(cmd, "echo")) { printf("%s\n", args); } else if (!strcmp(cmd, "exec")) { - libc_file *file = file_open(args, 0); + FILE *file = fopen(args, "r"); if (!file) { printf("couldn't open file\n"); } else { diff --git a/src/user/lib/elfload.c b/src/user/lib/elfload.c index a30fd7c..75c1162 100644 --- a/src/user/lib/elfload.c +++ b/src/user/lib/elfload.c @@ -7,7 +7,7 @@ #include <user/lib/elf.h> #include <user/lib/elfload.h> -void elf_execf(libc_file *f) { +void elf_execf(FILE *f) { const size_t cap = 0x60000; size_t pos = 0; void *buf = malloc(cap); // TODO a way to get file size diff --git a/src/user/lib/elfload.h b/src/user/lib/elfload.h index 8310b3f..d0fb29d 100644 --- a/src/user/lib/elfload.h +++ b/src/user/lib/elfload.h @@ -1,7 +1,7 @@ #pragma once #include <bits/file.h> -void elf_execf(libc_file *f); +void elf_execf(FILE *f); void elf_exec(void *elf); void *elf_partialexec(void *elf); /* returns pointer to entry point */ diff --git a/src/user/lib/include/bits/file.h b/src/user/lib/include/bits/file.h index d37b7de..74a8c17 100644 --- a/src/user/lib/include/bits/file.h +++ b/src/user/lib/include/bits/file.h @@ -1,9 +1,9 @@ #pragma once #include <stdbool.h> // TODO make opaque -struct libc_file { +struct FILE { int fd; int pos; bool eof; }; -typedef struct libc_file libc_file; +typedef struct FILE FILE; diff --git a/src/user/lib/include/errno.h b/src/user/lib/include/errno.h new file mode 100644 index 0000000..6686a01 --- /dev/null +++ b/src/user/lib/include/errno.h @@ -0,0 +1,3 @@ +#pragma once +#include <camellia/errno.h> +extern int errno; diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index 291a194..2b1d8ed 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -8,11 +8,11 @@ int snprintf(char *str, size_t len, const char *fmt, ...); int _klogf(const char *fmt, ...); // for kernel debugging only -extern libc_file *const stdin, *const stdout; +extern FILE *const stdin, *const stdout; -libc_file *file_open(const char *path, int flags); -libc_file *file_reopen(libc_file*, const char *path, int flags); -libc_file *file_clone(const libc_file*); -int file_read(libc_file*, char *buf, size_t len); -int file_write(libc_file*, const char *buf, size_t len); -void file_close(libc_file*); +FILE *fopen(const char *path, const char *mode); +FILE *freopen(const char *path, const char *mode, FILE*); +FILE *file_clone(const FILE*); +int file_read(FILE*, char *buf, size_t len); +int file_write(FILE*, const char *buf, size_t len); +void file_close(FILE*); diff --git a/src/user/lib/malloc.c b/src/user/lib/malloc.c index ea65d74..b732df9 100644 --- a/src/user/lib/malloc.c +++ b/src/user/lib/malloc.c @@ -26,7 +26,7 @@ void *malloc(size_t size) { } if (!iter) iter = expand(size); - if (!iter) return NULL; + if (!iter) return NULL; // TODO set errno iter->used = true; // TODO truncate and split diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c index c998de7..af653bb 100644 --- a/src/user/lib/stdlib.c +++ b/src/user/lib/stdlib.c @@ -1,4 +1,5 @@ #include <camellia/syscalls.h> +#include <errno.h> #include <shared/printf.h> #include <stdio.h> #include <stdlib.h> @@ -6,13 +7,15 @@ #include <unistd.h> // TODO oh god this garbage - malloc, actually open, [...] -static libc_file _stdin_null = { .fd = 0 }; -static libc_file _stdout_null = { .fd = 1 }; +static FILE _stdin_null = { .fd = 0 }; +static FILE _stdout_null = { .fd = 1 }; -libc_file *const stdin = &_stdin_null, *const stdout = &_stdout_null; +FILE *const stdin = &_stdin_null, *const stdout = &_stdout_null; + +int errno = 0; static void backend_file(void *arg, const char *buf, size_t len) { - file_write((libc_file*)arg, buf, len); + file_write((FILE*)arg, buf, len); } int printf(const char *fmt, ...) { @@ -58,30 +61,38 @@ int _klogf(const char *fmt, ...) { } -libc_file *file_open(const char *path, int flags) { - handle_t h = _syscall_open(path, strlen(path), flags); - libc_file *f; - if (h < 0) return NULL; +FILE *fopen(const char *path, const char *mode) { + FILE *f; + handle_t h; + int flags = 0; + if (mode[0] == 'w' || mode[0] == 'a') + flags |= OPEN_CREATE; + // TODO truncate on w + + h = _syscall_open(path, strlen(path), flags); + if (h < 0) { + errno = -h; + return NULL; + } f = malloc(sizeof *f); if (!f) { close(h); return NULL; } - f->pos = 0; + f->pos = mode[0] == 'a' ? -1 : 0; f->eof = false; f->fd = h; return f; } -libc_file *file_reopen(libc_file *f, const char *path, int flags) { + FILE *freopen(const char *path, const char *mode, FILE *f) { /* partially based on the musl implementation of freopen */ - libc_file *f2; + FILE *f2; if (!path) goto fail; - f2 = file_open(path, flags); + f2 = fopen(path, mode); if (!f2) goto fail; - /* shouldn't happen, but if it happens, let's roll with it. */ if (f->fd == f2->fd) f2->fd = -1; if (_syscall_dup(f2->fd, f->fd, 0) < 0) goto fail2; @@ -97,9 +108,9 @@ fail: return NULL; } -libc_file *file_clone(const libc_file *f) { +FILE *file_clone(const FILE *f) { handle_t h = _syscall_dup(f->fd, -1, 0); - libc_file *f2; + FILE *f2; if (h < 0) return NULL; // TODO file_wrapfd @@ -114,17 +125,22 @@ libc_file *file_clone(const libc_file *f) { return f2; } -int file_read(libc_file *f, char *buf, size_t len) { +int file_read(FILE *f, char *buf, size_t len) { if (f->fd < 0) return -1; int res = _syscall_read(f->fd, buf, len, f->pos); if (res < 0) return res; if (res == 0 && len > 0) f->eof = true; + + bool negative_pos = f->pos < 0; f->pos += res; + if (negative_pos && f->pos >= 0) + f->pos = -1; + return res; } -int file_write(libc_file *f, const char *buf, size_t len) { +int file_write(FILE *f, const char *buf, size_t len) { if (f->fd < 0) return -1; int res = _syscall_write(f->fd, buf, len, f->pos); @@ -133,7 +149,7 @@ int file_write(libc_file *f, const char *buf, size_t len) { return res; } -void file_close(libc_file *f) { +void file_close(FILE *f) { if (f->fd > 0) close(f->fd); if (f != &_stdin_null && f != &_stdout_null) free(f); |