diff options
author | Anton Kling <anton@kling.gg> | 2023-10-24 13:50:36 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2023-10-24 13:50:36 +0200 |
commit | 3ae251a204748a2f1c4c2af6c55e73d5e5159d0f (patch) | |
tree | 07d02eb1b25173e3af41284a9def4cdb9191bb97 /userland/libc/libc.c | |
parent | e91a4f1e31ee100b2a13cefedb182e9d69ef04ad (diff) |
LibC: Allocate stdin, stdout, stderr on the heap so they can be closed with fclose
Diffstat (limited to 'userland/libc/libc.c')
-rw-r--r-- | userland/libc/libc.c | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/userland/libc/libc.c b/userland/libc/libc.c index 259ef0c..90c0323 100644 --- a/userland/libc/libc.c +++ b/userland/libc/libc.c @@ -96,6 +96,41 @@ char *errno_strings[] = { #define TAB_SIZE 8 +FILE *__stdin_FILE; +FILE *__stdout_FILE; +FILE *__stderr_FILE; +#define stdin __stdin_FILE +#define stdout __stdout_FILE +#define stderr __stderr_FILE + +void _libc_setup(void) { + __stdin_FILE = malloc(sizeof(FILE)); + __stdin_FILE->write = write_fd; + __stdin_FILE->read = read_fd; + __stdin_FILE->seek = NULL; + __stdin_FILE->is_eof = 0; + __stdin_FILE->has_error = 0; + __stdin_FILE->cookie = NULL; + __stdin_FILE->fd = 0; + + __stdout_FILE = malloc(sizeof(FILE)); + __stdout_FILE->write = write_fd; + __stdout_FILE->read = read_fd; + __stdout_FILE->is_eof = 0; + __stdout_FILE->has_error = 0; + __stdout_FILE->seek = NULL; + __stdout_FILE->cookie = NULL; + __stdout_FILE->fd = 1; + + __stderr_FILE = malloc(sizeof(FILE)); + __stderr_FILE->write = write_fd; + __stderr_FILE->read = read_fd; + __stderr_FILE->is_eof = 0; + __stderr_FILE->has_error = 0; + __stderr_FILE->cookie = NULL; + __stderr_FILE->fd = 2; +} + // Functions preserve the registers ebx, esi, edi, ebp, and esp; while // eax, ecx, edx are scratch registers. @@ -176,12 +211,12 @@ int s_syscall(int sys) { } int write(int fd, const char *buf, size_t count) { - /* - struct SYS_WRITE_PARAMS args = { - .fd = fd, - .buf = buf, - .count = count, - };*/ + /* +struct SYS_WRITE_PARAMS args = { +.fd = fd, +.buf = buf, +.count = count, +};*/ return syscall(SYS_WRITE, fd, buf, count, 0, 0); } |