From 71811470be4f182b623510cf92ea5cfff16df172 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 28 Jan 2024 16:34:27 +0100 Subject: Add libsimple_generate_seed and libsimple_srand MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 3 +++ generate_seed.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ libsimple.h | 1 + libsimple/random.h | 22 ++++++++++++++++++++ srand.c | 18 +++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 generate_seed.c create mode 100644 libsimple/random.h create mode 100644 srand.c diff --git a/Makefile b/Makefile index cc99bc0..38f2537 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,7 @@ SUBHDR =\ libsimple/printf.h\ libsimple/pvalloc.h\ libsimple/pvallocz.h\ + libsimple/random.h\ libsimple/realloc.h\ libsimple/search.h\ libsimple/str.h\ @@ -224,6 +225,7 @@ OBJ =\ ewcsdup.o\ ewcsndup.o\ ewmemdup.o\ + generate_seed.o\ getenv_e.o\ getenv_ne.o\ gmtime.o\ @@ -318,6 +320,7 @@ OBJ =\ reallocf.o\ reallocfn.o\ reallocn.o\ + srand.o\ stpmove.o\ stpnmove.o\ stpnset.o\ diff --git a/generate_seed.c b/generate_seed.c new file mode 100644 index 0000000..cce274f --- /dev/null +++ b/generate_seed.c @@ -0,0 +1,59 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + +#include + + +unsigned int +libsimple_generate_seed(void) /* TODO add test */ +{ + uintptr_t longseed = 1; + void *ptr; + uintptr_t ptri; + unsigned int seed; + struct timespec ts; + uint8_t (*at_random)[16]; + size_t i; + + if (!clock_gettime(CLOCK_REALTIME, &ts)) + longseed ^= (uintptr_t)ts.tv_sec * (uintptr_t)1000000000UL + (uintptr_t)ts.tv_nsec; + + ptr = malloc(1); + ptri = (uintptr_t)ptr; + free(ptr); + longseed ^= ptri; + longseed ^= (uintptr_t)&ptri; + longseed ^= (uintptr_t)clock(); + + if (!clock_gettime(CLOCK_MONOTONIC_RAW, &ts)) + longseed ^= (uintptr_t)ts.tv_sec * (uintptr_t)1000000000UL + (uintptr_t)ts.tv_nsec; + + ptri = (uintptr_t)getauxval(AT_RANDOM); + if (ptri) { + at_random = (void *)ptri; + for (i = 0; i < ELEMSOF(*at_random); i++) + longseed ^= (*at_random)[i] << (i % sizeof(longseed) * (size_t)CHAR_BIT); + } + + seed = 0; + while (longseed) { + seed ^= (unsigned int)longseed; + longseed >>= sizeof(unsigned int) * (CHAR_BIT - 1); + longseed >>= sizeof(unsigned int); + } + + return seed; +} + + +#else +#include "test.h" + +int +main(void) +{ + return 0; +} + +#endif diff --git a/libsimple.h b/libsimple.h index 9cc0f8f..1cd7806 100644 --- a/libsimple.h +++ b/libsimple.h @@ -165,6 +165,7 @@ #include "libsimple/strn.h" #include "libsimple/strtoint.h" #include "libsimple/search.h" +#include "libsimple/random.h" /** diff --git a/libsimple/random.h b/libsimple/random.h new file mode 100644 index 0000000..3c09d7a --- /dev/null +++ b/libsimple/random.h @@ -0,0 +1,22 @@ +/* See LICENSE file for copyright and license details. */ + + +/** + * Creates a pseudo-random seed + * + * @return The pseudo-random seed + */ +LIBSIMPLE_GCC_ONLY__(__attribute__((__warn_unused_result__))) +unsigned int libsimple_generate_seed(void); /* TODO add man page */ + + +/** + * Wrapper for srand(3) that creates a pseudo-random + * seed with which it seeds the pseudo-random number + * generator + */ +inline void +libsimple_srand(void) /* TODO add man page */ +{ + srand(libsimple_generate_seed()); +} diff --git a/srand.c b/srand.c new file mode 100644 index 0000000..e1648cb --- /dev/null +++ b/srand.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +extern inline void libsimple_srand(void); /* TODO add test */ + + +#else +#include "test.h" + +int +main(void) +{ + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2