2022-10-11 21:08:46 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-10-12 17:14:49 +02:00
|
|
|
#include <unistd.h>
|
2022-10-11 21:08:46 +02:00
|
|
|
|
2022-10-12 20:41:55 +02:00
|
|
|
static void terminate_libc()
|
|
|
|
{
|
|
|
|
fclose(stdout);
|
|
|
|
fclose(stderr);
|
|
|
|
}
|
|
|
|
|
2022-10-11 21:08:46 +02:00
|
|
|
extern "C" void initialize_libc()
|
|
|
|
{
|
2022-10-12 20:19:45 +02:00
|
|
|
if (lseek(0, 0, SEEK_CUR) < 0)
|
|
|
|
{
|
|
|
|
if (errno == EBADF) stdout = fopen("/dev/console", "rw");
|
|
|
|
else
|
|
|
|
exit(errno);
|
|
|
|
if (!stdout) exit(errno);
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
else { stdout = fdopen(0, "rw"); }
|
|
|
|
if (lseek(1, 0, SEEK_CUR) < 0)
|
|
|
|
{
|
|
|
|
if (errno == EBADF) stderr = fopen("/dev/console", "rw");
|
|
|
|
else
|
|
|
|
exit(errno);
|
|
|
|
if (!stderr) exit(errno);
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
else { stderr = fdopen(1, "rw"); }
|
2022-10-12 20:41:55 +02:00
|
|
|
atexit(terminate_libc);
|
2022-10-11 21:08:46 +02:00
|
|
|
}
|