This is a GNU extension, but I'm fine with adding it to libc. It's guarded by the _GNU_SOURCE feature test macro anyways.
67 lines
1.6 KiB
C++
67 lines
1.6 KiB
C++
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
static void terminate_libc()
|
|
{
|
|
fclose(stdout);
|
|
fclose(stderr);
|
|
fclose(stdin);
|
|
}
|
|
|
|
static void initialize_random()
|
|
{
|
|
unsigned int seed = 3735928559U;
|
|
|
|
FILE* fp = fopen("/dev/random", "rw");
|
|
if (!fp)
|
|
{
|
|
errno = 0;
|
|
goto failed_to_read_dev_random;
|
|
}
|
|
|
|
fread(&seed, sizeof(seed), 1, fp);
|
|
if (ferror(fp))
|
|
{
|
|
errno = 0;
|
|
fclose(fp);
|
|
goto failed_to_read_dev_random;
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
failed_to_read_dev_random:
|
|
srand(seed); // If we failed, this will be seeded with a known value. Else, it will be seeded with a random value
|
|
// from the kernel.
|
|
return;
|
|
}
|
|
|
|
static void check_for_file(int fd, FILE** target_stream, const char* path, const char* mode)
|
|
{
|
|
if (lseek(fd, 0, SEEK_CUR) < 0)
|
|
{
|
|
if (errno == EBADF) *target_stream = fopen(path, mode);
|
|
else
|
|
exit(-127);
|
|
if (!*target_stream) exit(-127);
|
|
errno = 0;
|
|
}
|
|
else { *target_stream = fdopen(fd, mode); }
|
|
}
|
|
|
|
extern char* program_invocation_name;
|
|
|
|
extern "C" void initialize_libc(int argc, char** argv)
|
|
{
|
|
check_for_file(STDIN_FILENO, &stdin, "/dev/kbd", "r");
|
|
check_for_file(STDOUT_FILENO, &stdout, "/dev/console", "rw");
|
|
check_for_file(STDERR_FILENO, &stderr, "/dev/console", "rw");
|
|
|
|
program_invocation_name =
|
|
argv[0]; // argv[0] should only be nullptr when being init, which shouldn't use program_invocation_name anyways.
|
|
|
|
initialize_random();
|
|
atexit(terminate_libc);
|
|
} |