Skip to content

Commit

Permalink
rand(), lrand48() and random() are POSIX 2001
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelortmann committed Feb 16, 2023
1 parent 516a4a8 commit 86a2b61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 45 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 10 additions & 44 deletions src/eggdrop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 86a2b61

Please sign in to comment.