From 80ab982fe4e2b837c62bac6426f4b8eafecbfb88 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 11 Oct 2022 21:08:46 +0200 Subject: [PATCH] libc: make stdout and stderr functional what were before one extern FILE* without reference now are opened by libc on program initialization, to point to /dev/console by default. --- libs/libc/crt0.asm | 3 +++ libs/libc/include/stdio.h | 8 +++++--- libs/libc/src/init.cpp | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 libs/libc/src/init.cpp diff --git a/libs/libc/crt0.asm b/libs/libc/crt0.asm index be8192e0..24dfab0b 100644 --- a/libs/libc/crt0.asm +++ b/libs/libc/crt0.asm @@ -3,6 +3,7 @@ section .text extern _init extern main extern _fini +extern initialize_libc extern exit global _start @@ -13,6 +14,8 @@ _start: push rbp ; rbp=0 mov rbp, rsp + call initialize_libc + call _init mov rdi, 0 ; argc = 0 diff --git a/libs/libc/include/stdio.h b/libs/libc/include/stdio.h index ce5c7bbb..83023afb 100644 --- a/libs/libc/include/stdio.h +++ b/libs/libc/include/stdio.h @@ -13,13 +13,15 @@ typedef struct int f_err; } FILE; +extern FILE* __stderr; +extern FILE* __stdout; +#define stderr __stderr +#define stdout __stdout + #ifdef __cplusplus extern "C" { #endif - - extern FILE* stderr; -#define stderr stderr int fclose(FILE*); int fflush(FILE*); FILE* fopen(const char*, const char*); diff --git a/libs/libc/src/init.cpp b/libs/libc/src/init.cpp new file mode 100644 index 00000000..b17cbd46 --- /dev/null +++ b/libs/libc/src/init.cpp @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +extern "C" void initialize_libc() +{ + __stderr = fopen("/dev/console", "rw"); + if (!stderr) exit(errno); + __stdout = fopen("/dev/console", "rw"); + if (!stdout) exit(errno); + clearerr(__stderr); + clearerr(__stdout); +} \ No newline at end of file