From 7175ea4a12b7f2132b81d22179eabcecd3e41b17 Mon Sep 17 00:00:00 2001 From: hniksic Date: Tue, 27 Nov 2001 02:11:13 -0800 Subject: [PATCH] [svn] Check for random. Published in . --- ChangeLog | 4 ++++ configure.in | 2 +- src/ChangeLog | 5 +++++ src/cmpt.c | 35 +++++++++++++++++++++++++++++++++++ src/config.h.in | 3 +++ src/sysdep.h | 3 +++ 6 files changed, 51 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a3d0db08..526e02aa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2001-11-27 Hrvoje Niksic + + * configure.in: Check for random. + 2001-11-26 Hrvoje Niksic * configure.in: Check for usleep. diff --git a/configure.in b/configure.in index 1cc995a2..e853b770 100644 --- a/configure.in +++ b/configure.in @@ -175,7 +175,7 @@ AC_FUNC_MMAP AC_CHECK_FUNCS(strdup strstr strcasecmp strncasecmp strpbrk) AC_CHECK_FUNCS(gettimeofday mktime strptime) AC_CHECK_FUNCS(strerror snprintf vsnprintf select signal symlink access isatty) -AC_CHECK_FUNCS(uname gethostname usleep) +AC_CHECK_FUNCS(uname gethostname random usleep) dnl dnl Check if we need to compile in getopt.c. diff --git a/src/ChangeLog b/src/ChangeLog index 1bc8c0b8..e6c060dc 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2001-11-27 Hrvoje Niksic + + * cmpt.c (random): New function, a simple-minded replacement for + random() on systems that don't have it. + 2001-11-26 Hrvoje Niksic * config.h.in: Put a HAVE_USLEEP stub. diff --git a/src/cmpt.c b/src/cmpt.c index 7a7add09..54215131 100644 --- a/src/cmpt.c +++ b/src/cmpt.c @@ -1222,3 +1222,38 @@ usleep (unsigned long usec) return 0; } #endif /* not HAVE_USLEEP */ + + +#ifndef HAVE_RANDOM +/* For the systems without random: a really simple congruential RNG, + only good enough for what Wget uses it for. Before you panic: this + is not used for any kind of cryptography. */ + +static long random_seed; + +#define RANDOM_A 9301 +#define RANDOM_C 49297 +#define RANDOM_M 233280 + +static int +random_1 (void) +{ + if (!random_seed) + random_seed = time (NULL); + random_seed = (random_seed * RANDOM_A + RANDOM_C) % RANDOM_M; + return random_seed; +} + +long +random (void) +{ + /* Upper bits of random() are a bit more random. Compose random() + from higher bits of three call to random(). */ + unsigned r1 = random_1 () >> 8; + unsigned r2 = random_1 () >> 4; + unsigned r3 = random_1 (); + long result = r1 ^ r2 ^ r3; + + return result; +} +#endif /* not HAVE_RANDOM */ diff --git a/src/config.h.in b/src/config.h.in index 1364d27f..62793c2c 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -168,6 +168,9 @@ char *alloca (); /* Define if you have the usleep function. */ #undef HAVE_USLEEP +/* Define if you have the random function. */ +#undef HAVE_RANDOM + /* Define if you have the header file. */ #undef HAVE_STRING_H diff --git a/src/sysdep.h b/src/sysdep.h index 1c5deb1b..d4430b2b 100644 --- a/src/sysdep.h +++ b/src/sysdep.h @@ -166,6 +166,9 @@ int vsnprintf (); #ifndef HAVE_USLEEP int usleep (); #endif +#ifndef HAVE_RANDOM +long random (); +#endif /* SunOS brain damage -- for some reason, SunOS header files fail to declare the functions below, which causes all kinds of problems