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-14 19:36:20 +02:00
|
|
|
static void check_for_file(int fd, FILE** target_stream, const char* path, const char* mode)
|
2022-10-11 21:08:46 +02:00
|
|
|
{
|
2022-10-14 19:36:20 +02:00
|
|
|
if (lseek(fd, 0, SEEK_CUR) < 0)
|
2022-10-12 20:19:45 +02:00
|
|
|
{
|
2022-10-14 19:36:20 +02:00
|
|
|
if (errno == EBADF) *target_stream = fopen(path, mode);
|
2022-10-12 20:19:45 +02:00
|
|
|
else
|
|
|
|
exit(errno);
|
2022-10-14 19:36:20 +02:00
|
|
|
if (!*target_stream) exit(errno);
|
2022-10-12 20:19:45 +02:00
|
|
|
errno = 0;
|
|
|
|
}
|
2022-10-14 19:36:20 +02:00
|
|
|
else { *target_stream = fdopen(fd, mode); }
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void initialize_libc()
|
|
|
|
{
|
2022-10-14 21:24:18 +02:00
|
|
|
check_for_file(STDOUT_FILENO, &stdout, "/dev/console", "rw");
|
|
|
|
check_for_file(STDERR_FILENO, &stderr, "/dev/console", "rw");
|
2022-10-12 20:41:55 +02:00
|
|
|
atexit(terminate_libc);
|
2022-10-11 21:08:46 +02:00
|
|
|
}
|