libc: close fds 0 and 1 before opening stdout and stderr

This commit is contained in:
apio 2022-10-12 17:14:49 +02:00
parent 25a460e3c6
commit dfdc3b2d11

View File

@ -2,9 +2,13 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
extern "C" void initialize_libc() extern "C" void initialize_libc()
{ {
close(0); // If it was already open, close it
close(1); // If it was already open, close it
errno = 0; // If it was not open. the kernel will throw us EBADF. Let's ignore that, since we don't care.
__stderr = fopen("/dev/console", "rw"); __stderr = fopen("/dev/console", "rw");
if (!stderr) exit(errno); if (!stderr) exit(errno);
__stdout = fopen("/dev/console", "rw"); __stdout = fopen("/dev/console", "rw");