From 5c4fb3b3c58a2d850031e9449b5d65887e42f1c7 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 11 Jul 2022 17:29:31 +0200 Subject: init/stdlib: a more posix-y file api --- src/init/stdlib.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/init/stdlib.c') diff --git a/src/init/stdlib.c b/src/init/stdlib.c index d9d882d..a02eb7c 100644 --- a/src/init/stdlib.c +++ b/src/init/stdlib.c @@ -2,8 +2,7 @@ #include #include -libc_file __stdin = {.fd = -1}; -libc_file __stdout = {.fd = -1}; +libc_file *stdin, *stdout; static void backend_file(void *arg, const char *buf, size_t len) { file_write((libc_file*)arg, buf, len); @@ -13,7 +12,7 @@ int printf(const char *fmt, ...) { int ret = 0; va_list argp; va_start(argp, fmt); - ret = __printf_internal(fmt, argp, backend_file, (void*)&__stdout); + ret = __printf_internal(fmt, argp, backend_file, (void*)stdout); va_end(argp); return ret; } @@ -52,12 +51,20 @@ int _klogf(const char *fmt, ...) { } -int file_open(libc_file *f, const char *path, int flags) { +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; + + f = malloc(sizeof *f); + if (!f) { + _syscall_close(h); + return NULL; + } f->pos = 0; f->eof = false; - f->fd = _syscall_open(path, strlen(path), flags); - if (f->fd < 0) return f->fd; - return 0; + f->fd = h; + return f; } int file_read(libc_file *f, char *buf, size_t len) { @@ -81,4 +88,5 @@ int file_write(libc_file *f, const char *buf, size_t len) { void file_close(libc_file *f) { if (f->fd > 0) _syscall_close(f->fd); + free(f); } -- cgit v1.2.3