> 2023-11-09 Bruno Haible <br...@clisp.org> > > rand: Add tests. > * tests/test-rand.c: New file. > * modules/rand-tests: New file. > > rand: New module. > * lib/rand.c: New file, based on glibc/stdlib/rand.c. > * m4/rand.m4: New file. > * modules/rand: New file. > * doc/posix-functions/rand.texi: Mention the new module.
Somehow I forgot to apply the usual Gnulib idioms. Noticed it because REPLACE_RAND ends up being empty. Uh oh. Here's the patch that fixes it. 2023-11-21 Bruno Haible <br...@clisp.org> rand: Use the usual patterns for overriding a function. * lib/stdlib.in.h (RAND_MAX): Override also if module 'rand' is present. (rand): New declaration. * m4/stdlib_h.m4 (gl_STDLIB_H_REQUIRE_DEFAULTS): Initialize GNULIB_RAND. (gl_STDLIB_H_DEFAULTS): Initialize REPLACE_RAND. * modules/stdlib (Makefile.am): Substitute GNULIB_RAND and REPLACE_RAND. diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h index b27d2c08c6..ea4c3e6b63 100644 --- a/lib/stdlib.in.h +++ b/lib/stdlib.in.h @@ -1122,11 +1122,26 @@ _GL_WARN_ON_USE (qsort_r, "qsort_r is not portable - " #endif -#if @GNULIB_RANDOM_R@ -# if !@HAVE_RANDOM_R@ -# ifndef RAND_MAX -# define RAND_MAX 2147483647 +#if @GNULIB_RAND@ || (@GNULIB_RANDOM_R@ && !@HAVE_RANDOM_R@) +# ifndef RAND_MAX +# define RAND_MAX 2147483647 +# endif +#endif + + +#if @GNULIB_RAND@ +# if @REPLACE_RAND@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef rand +# define rand rpl_rand # endif +_GL_FUNCDECL_RPL (rand, int, (void)); +_GL_CXXALIAS_RPL (rand, int, (void)); +# else +_GL_CXXALIAS_SYS (rand, int, (void)); +# endif +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (rand); # endif #endif diff --git a/m4/stdlib_h.m4 b/m4/stdlib_h.m4 index f47c1eb37b..6f9e992aef 100644 --- a/m4/stdlib_h.m4 +++ b/m4/stdlib_h.m4 @@ -1,4 +1,4 @@ -# stdlib_h.m4 serial 75 +# stdlib_h.m4 serial 76 dnl Copyright (C) 2007-2023 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -134,6 +134,7 @@ AC_DEFUN([gl_STDLIB_H_REQUIRE_DEFAULTS] gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_PTSNAME_R]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_PUTENV]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_QSORT_R]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_RAND]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_RANDOM]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_RANDOM_R]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_REALLOCARRAY]) @@ -237,6 +238,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS] REPLACE_PTSNAME_R=0; AC_SUBST([REPLACE_PTSNAME_R]) REPLACE_PUTENV=0; AC_SUBST([REPLACE_PUTENV]) REPLACE_QSORT_R=0; AC_SUBST([REPLACE_QSORT_R]) + REPLACE_RAND=0; AC_SUBST([REPLACE_RAND]) REPLACE_RANDOM=0; AC_SUBST([REPLACE_RANDOM]) REPLACE_RANDOM_R=0; AC_SUBST([REPLACE_RANDOM_R]) REPLACE_REALLOC_FOR_REALLOC_GNU=0; AC_SUBST([REPLACE_REALLOC_FOR_REALLOC_GNU]) diff --git a/modules/stdlib b/modules/stdlib index 23229103e7..baf4b29a8c 100644 --- a/modules/stdlib +++ b/modules/stdlib @@ -62,6 +62,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \ -e 's/@''GNULIB_PTSNAME_R''@/$(GNULIB_PTSNAME_R)/g' \ -e 's/@''GNULIB_PUTENV''@/$(GNULIB_PUTENV)/g' \ -e 's/@''GNULIB_QSORT_R''@/$(GNULIB_QSORT_R)/g' \ + -e 's/@''GNULIB_RAND''@/$(GNULIB_RAND)/g' \ -e 's/@''GNULIB_RANDOM''@/$(GNULIB_RANDOM)/g' \ -e 's/@''GNULIB_RANDOM_R''@/$(GNULIB_RANDOM_R)/g' \ -e 's/@''GNULIB_REALLOC_GNU''@/$(GNULIB_REALLOC_GNU)/g' \ @@ -159,6 +160,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \ -e 's|@''REPLACE_PTSNAME_R''@|$(REPLACE_PTSNAME_R)|g' \ -e 's|@''REPLACE_PUTENV''@|$(REPLACE_PUTENV)|g' \ -e 's|@''REPLACE_QSORT_R''@|$(REPLACE_QSORT_R)|g' \ + -e 's|@''REPLACE_RAND''@|$(REPLACE_RAND)|g' \ -e 's|@''REPLACE_RANDOM''@|$(REPLACE_RANDOM)|g' \ -e 's|@''REPLACE_RANDOM_R''@|$(REPLACE_RANDOM_R)|g' \ -e 's|@''REPLACE_REALLOC_FOR_REALLOC_GNU''@|$(REPLACE_REALLOC_FOR_REALLOC_GNU)|g' \