aboutsummaryrefslogtreecommitdiff
path: root/stdlib/stdlib.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-11-03 22:37:48 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-11-03 22:37:48 -0500
commitfc9a6f1416be514e9d26b301d05e7e347560560b (patch)
tree7d61cc3657c36dde05135f17dbf5923cff177abf /stdlib/stdlib.c
parent52e3d3fe6f2c3e5051affe155fed364d1a5d623c (diff)
Add RNGs to the language
Diffstat (limited to 'stdlib/stdlib.c')
-rw-r--r--stdlib/stdlib.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/stdlib/stdlib.c b/stdlib/stdlib.c
index fb37fcfe..f2c6897c 100644
--- a/stdlib/stdlib.c
+++ b/stdlib/stdlib.c
@@ -2,6 +2,7 @@
#include <errno.h>
#include <execinfo.h>
+#include <fcntl.h>
#include <gc.h>
#include <stdbool.h>
#include <stdint.h>
@@ -17,6 +18,7 @@
#include "metamethods.h"
#include "patterns.h"
#include "paths.h"
+#include "rng.h"
#include "siphash.h"
#include "stdlib.h"
#include "tables.h"
@@ -30,14 +32,15 @@ public void tomo_init(void)
GC_INIT();
USE_COLOR = getenv("COLOR") ? strcmp(getenv("COLOR"), "1") == 0 : isatty(STDOUT_FILENO);
getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0);
- unsigned int seed;
- getrandom(&seed, sizeof(seed), 0);
- srand(seed);
- srand48(seed);
-
- long long_seed;
- getrandom(&long_seed, sizeof(long_seed), 0);
- Int$init_random(long_seed);
+
+ int rng_fd = open("/dev/urandom", O_RDONLY);
+ if (rng_fd < 0)
+ fail("Couldn't read from /dev/urandom");
+ uint8_t *random_bytes = GC_MALLOC_ATOMIC(40);
+ if (read(rng_fd, (void*)random_bytes, 40) < 40)
+ fail("Couldn't read from /dev/urandom");
+ Array_t rng_seed = {.length=40, .data=random_bytes, .stride=1, .atomic=1};
+ default_rng = RNG$new(rng_seed);
if (register_printf_specifier('k', printf_text, printf_text_size))
errx(1, "Couldn't set printf specifier");