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.
This commit is contained in:
apio 2022-10-11 21:08:46 +02:00
parent 53a4b3b85e
commit 80ab982fe4
3 changed files with 22 additions and 3 deletions

View File

@ -3,6 +3,7 @@ section .text
extern _init extern _init
extern main extern main
extern _fini extern _fini
extern initialize_libc
extern exit extern exit
global _start global _start
@ -13,6 +14,8 @@ _start:
push rbp ; rbp=0 push rbp ; rbp=0
mov rbp, rsp mov rbp, rsp
call initialize_libc
call _init call _init
mov rdi, 0 ; argc = 0 mov rdi, 0 ; argc = 0

View File

@ -13,13 +13,15 @@ typedef struct
int f_err; int f_err;
} FILE; } FILE;
extern FILE* __stderr;
extern FILE* __stdout;
#define stderr __stderr
#define stdout __stdout
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
extern FILE* stderr;
#define stderr stderr
int fclose(FILE*); int fclose(FILE*);
int fflush(FILE*); int fflush(FILE*);
FILE* fopen(const char*, const char*); FILE* fopen(const char*, const char*);

14
libs/libc/src/init.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
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);
}