Luna/libs/libc/src/rand.cpp

37 lines
793 B
C++
Raw Normal View History

2022-10-15 14:31:08 +00:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static FILE* randfp = nullptr;
void close_randfp()
{
fclose(randfp);
}
extern "C"
{
int rand() // FIXME: This function is a dirty and extremely hacky way to get going, /dev/random should only be used
// for initial seeding, userspace should do their own random calculations.
{
if (!randfp)
{
randfp = fopen("/dev/random", "rw");
assert(randfp != nullptr);
atexit(close_randfp);
}
int result;
fread(&result, sizeof(result), 1, randfp);
if (ferror(randfp))
{
perror("read(randfp)");
abort();
}
return result;
}
void srand(unsigned int)
{
return;
}
}