diff options
-rw-r--r-- | src/init/main.c | 26 | ||||
-rw-r--r-- | src/init/shell.c | 15 | ||||
-rw-r--r-- | src/init/stdlib.c | 22 | ||||
-rw-r--r-- | src/init/stdlib.h | 4 |
4 files changed, 40 insertions, 27 deletions
diff --git a/src/init/main.c b/src/init/main.c index 705da8f..1674f8c 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -19,7 +19,7 @@ int main(void) { // allocate bss _syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT); - file_open(&__stdout, "/com1", 0); + stdout = file_open("/com1", 0); printf("preinit\n"); /* move everything provided by the kernel to /kdev */ @@ -37,8 +37,6 @@ int main(void) { MOUNT("/bind/", fs_passthru(NULL)); - file_close(&__stdout); - if (_syscall_fork(0, NULL)) { /* (used to) expose a bug in the kernel * the program will flow like this: @@ -57,8 +55,12 @@ int main(void) { } if (!_syscall_fork(0, NULL)) { - if (file_open(&__stdout, "/kdev/com1", 0) < 0 || file_open(&__stdin, "/kdev/com1", 0) < 0) + libc_file *new = file_open("/kdev/com1", 0); + if (!new) { + printf("couldn't open /kdev/com1\n"); _syscall_exit(1); + } + stdout = stdin = new; // TODO file_clone, this is bad shell_loop(); _syscall_exit(1); @@ -66,10 +68,17 @@ int main(void) { if (!_syscall_fork(0, NULL)) { - if (file_open(&__stdout, "/vga_tty", 0) < 0) + libc_file *new; + new = file_open("/vga_tty", 0); + if (!new) { + printf("couldn't open /vga_tty\n"); _syscall_exit(1); + } + file_close(stdout); + stdout = new; - if (file_open(&__stdin, "/keyboard", 0) < 0) { + stdin = file_open("/keyboard", 0); + if (!stdin) { printf("couldn't open /keyboard\n"); _syscall_exit(1); } @@ -78,11 +87,6 @@ int main(void) { _syscall_exit(1); } - - // try to find any working output - if (file_open(&__stdout, "/kdev/com1", 0) < 0) - file_open(&__stdout, "/kdev/vga_tty", 0); - _syscall_await(); printf("init: quitting\n"); _syscall_exit(0); diff --git a/src/init/shell.c b/src/init/shell.c index 17cfffc..a4e96f9 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 (file_read(&__stdin, &c, 1)) { + while (file_read(stdin, &c, 1)) { switch (c) { case '\b': case 0x7f: @@ -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; + libc_file *file; static char buf[512]; int len; // first used for strlen(args), then length of buffer @@ -63,22 +63,23 @@ static void cmd_cat_ls(const char *args, bool ls) { } } - if (file_open(&file, buf, 0) < 0) { + file = file_open(buf, 0); + if (!file) { printf("couldn't open.\n"); return; } - while (!file.eof) { - int len = file_read(&file, buf, sizeof buf); + while (!file->eof) { + int len = file_read(file, buf, sizeof buf); if (len <= 0) break; if (ls) { for (int i = 0; i < len; i++) if (buf[i] == '\0') buf[i] = '\n'; } - file_write(&__stdout, buf, len); + file_write(stdout, buf, len); } - file_close(&file); + file_close(file); } static void cmd_hexdump(const char *args) { 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 <shared/printf.h> #include <shared/syscalls.h> -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); } diff --git a/src/init/stdlib.h b/src/init/stdlib.h index a48391c..f7917da 100644 --- a/src/init/stdlib.h +++ b/src/init/stdlib.h @@ -14,9 +14,9 @@ typedef struct { int pos; bool eof; } libc_file; -int file_open(libc_file*, const char *path, int flags); // TODO return a libc_file* +libc_file *file_open(const char *path, int flags); 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; +extern libc_file *stdin, *stdout; |