summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2022-05-01 19:13:17 +0200
committerdzwdz2022-05-01 19:13:17 +0200
commit681e6b45b6f22f30d29745fa9a400558fe6dada5 (patch)
treef0c845cf0410ed8deee4eafc5ca38739f31f5b53 /src
parent935eccb23be9464091ea0f6be3873c6450ff040c (diff)
init/libc: libc_file wrapper over the raw syscalls
Diffstat (limited to 'src')
-rw-r--r--src/init/main.c15
-rw-r--r--src/init/shell.c20
-rw-r--r--src/init/stdlib.c40
-rw-r--r--src/init/stdlib.h15
4 files changed, 63 insertions, 27 deletions
diff --git a/src/init/main.c b/src/init/main.c
index 488ec67..5601526 100644
--- a/src/init/main.c
+++ b/src/init/main.c
@@ -46,8 +46,8 @@ int main(void) {
}
if (!_syscall_fork()) {
- __stdin = __stdout = _syscall_open(argify("/com1"));
- if (__stdout < 0) _syscall_exit(1);
+ if (file_open(&__stdout, "/com1") < 0 || file_open(&__stdin, "/com1") < 0)
+ _syscall_exit(1);
shell_loop();
_syscall_exit(1);
@@ -55,11 +55,10 @@ int main(void) {
if (!_syscall_fork()) {
- __stdout = _syscall_open(argify("/vga_tty"));
- if (__stdout < 0) _syscall_exit(1);
+ if (file_open(&__stdout, "/vga_tty") < 0)
+ _syscall_exit(1);
- __stdin = _syscall_open(argify("/keyboard"));
- if (__stdin < 0) {
+ if (file_open(&__stdin, "/keyboard") < 0) {
printf("couldn't open /keyboard\n");
_syscall_exit(1);
}
@@ -70,8 +69,8 @@ int main(void) {
// try to find any working output
- __stdout = _syscall_open(argify("/com1"));
- if (__stdout < 0) __stdout = _syscall_open(argify("/vga_tty"));
+ if (file_open(&__stdout, "/com1") < 0)
+ file_open(&__stdout, "/vga_tty");
_syscall_await();
printf("init: quitting\n");
diff --git a/src/init/shell.c b/src/init/shell.c
index 7714c38..d723731 100644
--- a/src/init/shell.c
+++ b/src/init/shell.c
@@ -18,7 +18,7 @@ static char *split(char *base) {
static int readline(char *buf, size_t max) {
char c;
size_t pos = 0;
- while (_syscall_read(__stdin, &c, 1, 0)) {
+ while (file_read(&__stdin, &c, 1)) {
switch (c) {
case '\b':
case 0x7f:
@@ -35,7 +35,7 @@ static int readline(char *buf, size_t max) {
return pos;
default:
if (pos < max) {
- _syscall_write(__stdout, &c, 1, 0);
+ printf("%c", c);
buf[pos] = c;
pos++;
}
@@ -45,10 +45,9 @@ static int readline(char *buf, size_t max) {
}
static void cmd_cat_ls(const char *args, bool ls) {
- int fd;
+ libc_file file;
static char buf[512];
int len; // first used for strlen(args), then length of buffer
- size_t pos = 0;
if (!args) args = "/";
len = strlen(args);
@@ -64,25 +63,22 @@ static void cmd_cat_ls(const char *args, bool ls) {
}
}
- fd = _syscall_open(buf, len);
- if (fd < 0) {
+ if (file_open(&file, buf) < 0) {
printf("couldn't open.\n");
return;
}
- while (true) {
- len = _syscall_read(fd, buf, sizeof buf, pos);
+ while (!file.eof) {
+ int len = file_read(&file, buf, sizeof buf);
if (len <= 0) break;
- pos += len;
if (ls) {
for (int i = 0; i < len; i++)
if (buf[i] == '\0') buf[i] = '\n';
}
- _syscall_write(__stdout, buf, len, 0);
+ file_write(&__stdout, buf, len);
}
-
- _syscall_close(fd);
+ file_close(&file);
}
static void cmd_hexdump(const char *args) {
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);
+}
diff --git a/src/init/stdlib.h b/src/init/stdlib.h
index 1d7efdf..f8ab6c0 100644
--- a/src/init/stdlib.h
+++ b/src/init/stdlib.h
@@ -1,7 +1,18 @@
#pragma once
#include <shared/mem.h>
+#include <stdbool.h>
#include <stddef.h>
-extern int __stdin, __stdout;
-
int printf(const char *fmt, ...);
+
+typedef struct {
+ int fd;
+ int pos;
+ bool eof;
+} libc_file;
+int file_open(libc_file*, const char *path); // TODO return a 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*);
+
+extern libc_file __stdin, __stdout;