]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libgcobol: Only use random_r if it is available [PR119295]
authorIain Sandoe <iain@sandoe.co.uk>
Sun, 16 Mar 2025 09:32:12 +0000 (09:32 +0000)
committerIain Sandoe <iain@sandoe.co.uk>
Thu, 3 Apr 2025 13:33:27 +0000 (14:33 +0100)
We do not have a replacement at the moment, so fall back to using
regular random and friends.

PR cobol/119295

libgcobol/ChangeLog:

* config.h.in: Regenerate.
* configure: Regenerate.
* configure.ac: Configure random_r and friends
* intrinsic.cc (__gg__random): Use random_r when available.
(__gg__random_next): Likewise.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
libgcobol/config.h.in
libgcobol/configure
libgcobol/configure.ac
libgcobol/intrinsic.cc

index a7e5675c43c8c46b3489ebe4dcef1b5e0b99d71e..b201266e0610a8369482b3b5def48a06ef2a0dc5 100644 (file)
@@ -9,6 +9,9 @@
 /* Define if you have the iconv() function and it works. */
 #undef HAVE_ICONV
 
+/* Define to 1 if you have the `initstate_r' function. */
+#undef HAVE_INITSTATE_R
+
 /* Define to 1 if you have the <inttypes.h> header file. */
 #undef HAVE_INTTYPES_H
 
 /* Define to 1 if you have the <memory.h> header file. */
 #undef HAVE_MEMORY_H
 
+/* Define to 1 if you have the `random_r' function. */
+#undef HAVE_RANDOM_R
+
+/* Define to 1 if you have the `setstate_r' function. */
+#undef HAVE_SETSTATE_R
+
+/* Define to 1 if you have the `srandom_r' function. */
+#undef HAVE_SRANDOM_R
+
 /* Define to 1 if you have the <stdint.h> header file. */
 #undef HAVE_STDINT_H
 
index 1db4e792e030f87a34a8fba4675913488de0e388..44190d7e2fe0ffbea9c7d41d7cae6b57945b4816 100755 (executable)
@@ -629,6 +629,7 @@ ac_includes_default="\
 # include <unistd.h>
 #endif"
 
+ac_func_list=
 ac_unique_file="Makefile.am"
 ac_subst_vars='am__EXEEXT_FALSE
 am__EXEEXT_TRUE
@@ -2515,6 +2516,10 @@ $as_echo "$as_me: creating cache $cache_file" >&6;}
   >$cache_file
 fi
 
+as_fn_append ac_func_list " random_r"
+as_fn_append ac_func_list " srandom_r"
+as_fn_append ac_func_list " initstate_r"
+as_fn_append ac_func_list " setstate_r"
 # Check that the precious variables saved in the cache have kept the same
 # value.
 ac_cache_corrupted=false
@@ -12901,7 +12906,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 12904 "configure"
+#line 12909 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -13007,7 +13012,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 13010 "configure"
+#line 13015 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
 
 
 
+# These are GLIBC
+
+
+
+  for ac_func in $ac_func_list
+do :
+  as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
+ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
+if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
+  cat >>confdefs.h <<_ACEOF
+#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
+_ACEOF
+
+fi
+done
+
+
+
+
+
+
+
+
+
+
+
 if test "${multilib}" = "yes"; then
   multilib_arg="--enable-multilib"
 else
index e27621d03680a4c9c08a09e5eb528502e2e091bd..383b4134b130600f4c739b64638ac291a73e008f 100644 (file)
@@ -215,6 +215,9 @@ AM_CONDITIONAL([ENABLE_DARWIN_AT_RPATH], [test x$enable_darwin_at_rpath = xyes])
 AC_SUBST(enable_shared)
 AC_SUBST(enable_static)
 
+# These are GLIBC
+AC_CHECK_FUNCS_ONCE(random_r srandom_r initstate_r setstate_r)
+
 if test "${multilib}" = "yes"; then
   multilib_arg="--enable-multilib"
 else
index 16bf84be620b5459db977b0ac5039f543f6f112e..4bce481b0c040b587977eff27d32364dc2d08c20 100644 (file)
@@ -44,6 +44,8 @@
 #include <langinfo.h>
 #include <string.h>
 
+#include "config.h"
+
 #include "ec.h"
 #include "common-defs.h"
 #include "io.h"
@@ -3409,9 +3411,13 @@ __gg__trim( cblc_field_t *dest,
     }
   }
 
+#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R
 static struct random_data *buf = NULL;
 static char *state = NULL;
 static const size_t state_len = 256;
+#else
+static unsigned seed = 0;
+#endif
 
 extern "C"
 void
@@ -3420,6 +3426,9 @@ __gg__random( cblc_field_t *dest,
               size_t        input_offset,
               size_t        input_size)
   {
+  int32_t retval_31;
+  int rdigits;
+#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R
   // This creates a thread-safe pseudo-random number generator
   // using input as the seed
 
@@ -3436,16 +3445,21 @@ __gg__random( cblc_field_t *dest,
     __gg__clock_gettime(CLOCK_REALTIME, &ts);
     initstate_r( ts.tv_nsec, state, state_len, buf);
     }
-
-  int rdigits;
   int seed = (int)__gg__binary_value_from_qualified_field(&rdigits,
                                                           input,
                                                           input_offset,
                                                           input_size);
   srandom_r(seed, buf);
 
-  int32_t retval_31;
   random_r(buf, &retval_31);
+#else
+  seed = (unsigned)__gg__binary_value_from_qualified_field(&rdigits,
+                                                          input,
+                                                          input_offset,
+                                                          input_size);
+  srandom (seed);
+  retval_31 = random ();
+#endif
   // We are going to convert this to a value between zero and not quite one:
   double retval = double(retval_31) / double(0x80000000UL);
   __gg__double_to_target( dest,
@@ -3457,6 +3471,8 @@ extern "C"
 void
 __gg__random_next(cblc_field_t *dest)
   {
+  int32_t retval_31;
+#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R
   // The return value is between zero and not quite one
 
   if( !buf )
@@ -3469,9 +3485,10 @@ __gg__random_next(cblc_field_t *dest)
     __gg__clock_gettime(CLOCK_REALTIME, &ts);
     initstate_r( ts.tv_nsec, state, state_len, buf);
     }
-  int32_t retval_31;
   random_r(buf, &retval_31);
-
+#else
+  retval_31 = random ();
+#endif
   // We are going to convert this to a value between zero and not quite one:
   double retval = double(retval_31) / double(0x80000000UL);
   __gg__double_to_target( dest,