summaryrefslogtreecommitdiff
path: root/src/init/stdlib.c
diff options
context:
space:
mode:
authordzwdz2022-05-01 19:13:17 +0200
committerdzwdz2022-05-01 19:13:17 +0200
commit681e6b45b6f22f30d29745fa9a400558fe6dada5 (patch)
treef0c845cf0410ed8deee4eafc5ca38739f31f5b53 /src/init/stdlib.c
parent935eccb23be9464091ea0f6be3873c6450ff040c (diff)
init/libc: libc_file wrapper over the raw syscalls
Diffstat (limited to 'src/init/stdlib.c')
-rw-r--r--src/init/stdlib.c40
1 files changed, 35 insertions, 5 deletions
diff --git a/src/init/stdlib.c b/src/init/stdlib.c
index e928d2c..f59635e 100644
--- a/src/init/stdlib.c
+++ b/src/init/stdlib.c
@@ -2,19 +2,49 @@
#include <shared/printf.h>
#include <shared/syscalls.h>
-int __stdin = -1;
-int __stdout = -1;
+libc_file __stdin = {.fd = -1};
+libc_file __stdout = {.fd = -1};
static void backend_file(void *arg, const char *buf, size_t len) {
- _syscall_write(*(handle_t*)arg, buf, len, -1);
+ file_write((libc_file*)arg, buf, len);
}
int printf(const char *fmt, ...) {
int ret = 0;
va_list argp;
va_start(argp, fmt);
- if (__stdout >= 0)
- ret = __printf_internal(fmt, argp, backend_file, (void*)&__stdout);
+ ret = __printf_internal(fmt, argp, backend_file, (void*)&__stdout);
va_end(argp);
return ret;
}
+
+int file_open(libc_file *f, const char *path) {
+ f->pos = 0;
+ f->eof = false;
+ f->fd = _syscall_open(path, strlen(path));
+ if (f->fd < 0) return f->fd;
+ return 0;
+}
+
+int file_read(libc_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;
+ f->pos += res;
+ return res;
+}
+
+int file_write(libc_file *f, const char *buf, size_t len) {
+ if (f->fd < 0) return -1;
+
+ int res = _syscall_write(f->fd, buf, len, f->pos);
+ if (res < 0) return res;
+ f->pos += res;
+ return res;
+}
+
+void file_close(libc_file *f) {
+ if (f->fd > 0) _syscall_close(f->fd);
+}