Add VERY HACKY rand() implementation.

This commit is contained in:
apio 2022-10-15 16:31:08 +02:00
parent dd358eca29
commit 18f1f8b7ca
2 changed files with 43 additions and 0 deletions

View File

@ -50,6 +50,12 @@ extern "C"
* is undefined behavior. */
void free(void* ptr);
/* Returns a random number. */
int rand(void);
/* Does nothing. */
void srand(unsigned int seed);
#ifdef __cplusplus
}
#endif

37
libs/libc/src/rand.cpp Normal file
View File

@ -0,0 +1,37 @@
#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;
}
}