Add VERY HACKY rand() implementation.
This commit is contained in:
parent
dd358eca29
commit
18f1f8b7ca
@ -50,6 +50,12 @@ extern "C"
|
|||||||
* is undefined behavior. */
|
* is undefined behavior. */
|
||||||
void free(void* ptr);
|
void free(void* ptr);
|
||||||
|
|
||||||
|
/* Returns a random number. */
|
||||||
|
int rand(void);
|
||||||
|
|
||||||
|
/* Does nothing. */
|
||||||
|
void srand(unsigned int seed);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
37
libs/libc/src/rand.cpp
Normal file
37
libs/libc/src/rand.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user