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