From 86a2b619b7108fdab7be9a54c0b6c62083d9c8c0 Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Fri, 17 Feb 2023 00:59:13 +0100 Subject: [PATCH] rand(), lrand48() and random() are POSIX 2001 --- configure.ac | 2 +- src/eggdrop.h | 54 ++++++++++----------------------------------------- 2 files changed, 11 insertions(+), 45 deletions(-) diff --git a/configure.ac b/configure.ac index defe04557..795db4ba4 100644 --- a/configure.ac +++ b/configure.ac @@ -113,7 +113,7 @@ AX_TYPE_SOCKLEN_T AX_CREATE_STDINT_H([eggint.h]) # Checks for functions and their arguments. -AC_CHECK_FUNCS([dprintf explicit_bzero explicit_memset getrandom inet_aton isascii memset_s random rand lrand48 snprintf strlcpy vsnprintf]) +AC_CHECK_FUNCS([dprintf explicit_bzero explicit_memset getrandom inet_aton isascii memset_s snprintf strlcpy vsnprintf]) AC_FUNC_SELECT_ARGTYPES EGG_FUNC_B64_NTOP AC_FUNC_MMAP diff --git a/src/eggdrop.h b/src/eggdrop.h index 0b7015648..177ae7048 100644 --- a/src/eggdrop.h +++ b/src/eggdrop.h @@ -199,7 +199,7 @@ /* Yikes...who would have thought finding a usable random() would be so much * trouble? - * Note: random(), rand(), and lrand48() are *not* thread safe. + * Note: random() is *not* thread safe. * * QNX doesn't include random() and srandom() in libc.so, only in libc.a * So we can only use these functions in static builds on QNX. @@ -209,49 +209,15 @@ # undef HAVE_SRANDOM #endif -#ifdef HAVE_RANDOM - /* On systems with random(), RANDOM_MAX may or may not be defined. - * - * If RANDOM_MAX isn't defined, we use 0x7FFFFFFF (2^31-1), or 2147483647 - * since this follows the 4.3BSD and POSIX.1-2001 standards. This of course - * assumes random() uses a 32 bit long int type per the standards. - */ -# ifndef RANDOM_MAX -# define RANDOM_MAX 0x7FFFFFFF /* random() -- 2^31-1 */ -# endif -#else /* !HAVE_RANDOM */ - /* This shouldn't exist in this case, but just to be safe... */ -# ifdef RANDOM_MAX -# undef RANDOM_MAX -# endif - /* If we don't have random() it's safe to assume we also don't have - * srandom(), and we need both. - */ -# ifdef HAVE_RAND -# define random() rand() -# define srandom(x) srand(x) - /* Depending on the system int size, RAND_MAX can be either 0x7FFFFFFF - * (2^31-1), or 2147483647 for a 32 bit int, or 0x7FFF (2^15-1), or - * 32767 for a 16 bit int. The standards only state that RAND_MAX must - * be _at least_ 32767 but some systems with 16 bit int define it as - * 32767. See: SVr4, 4.3BSD, C89, C99, POSIX.1-2001. - */ -# define RANDOM_MAX RAND_MAX /* rand() -- 2^31-1 or 2^15-1 */ -# else /* !HAVE_RAND */ -# ifdef HAVE_LRAND48 -# define random() lrand48() -# define srandom(x) srand48(x) - /* For lrand48() we define RANDOM_MAX as 0x7FFFFFFF (2^31-1), or - * 2147483647 since this is what the SVr4 and POSIX.1-2001 standards - * call for. Note: SVID 3 declares these functions as obsolete and - * states rand() should be used instead. - */ -# define RANDOM_MAX 0x7FFFFFFF /* lrand48() -- 2^31-1 */ -# else /* !HAVE_LRAND48 */ -# include "Error: Must define one of HAVE_RANDOM, HAVE_RAND, or HAVE_LRAND48" -# endif /* HAVE_LRAND48 */ -# endif /* HAVE_RAND */ -#endif /* HAVE_RANDOM */ +/* On systems with random(), RANDOM_MAX may or may not be defined. + * + * If RANDOM_MAX isn't defined, we use 0x7FFFFFFF (2^31-1), or 2147483647 + * since this follows the 4.3BSD and POSIX.1-2001 standards. This of course + * assumes random() uses a 32 bit long int type per the standards. + */ +#ifndef RANDOM_MAX +# define RANDOM_MAX 0x7FFFFFFF /* random() -- 2^31-1 */ +#endif /* Use high-order bits for getting the random integer. With a modern