diff options
author | Andrey A. Chernov <ache@FreeBSD.org> | 1997-06-14 00:14:29 +0000 |
---|---|---|
committer | Andrey A. Chernov <ache@FreeBSD.org> | 1997-06-14 00:14:29 +0000 |
commit | 96c31b26181a194d24da9e7f89671ffa1e9d92db (patch) | |
tree | 6aa8fbc7f9c26c0a900ee0467b7643a1b2ffe3f0 /lib/libc/stdlib/random.c | |
parent | 854d14213ed3308ac581f9ced05328baddee5944 (diff) |
Notes
Diffstat (limited to 'lib/libc/stdlib/random.c')
-rw-r--r-- | lib/libc/stdlib/random.c | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c index 965142cc46d9..2c4e2347cbde 100644 --- a/lib/libc/stdlib/random.c +++ b/lib/libc/stdlib/random.c @@ -30,7 +30,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: random.c,v 1.8 1997/03/29 19:55:03 ache Exp $ * */ @@ -38,6 +38,7 @@ static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95"; #endif /* LIBC_SCCS and not lint */ +#include <sys/time.h> /* for srandomdev() */ #include <fcntl.h> /* for srandomdev() */ #include <stdio.h> #include <stdlib.h> @@ -285,10 +286,10 @@ srandom(x) * state buffer are no longer derived from the LC algorithm applied to * a fixed seed. */ -int +void srandomdev() { - int fd; + int fd, done; size_t len; if (rand_type == TYPE_0) @@ -296,20 +297,26 @@ srandomdev() else len = rand_deg * sizeof state[0]; + done = 0; fd = open("/dev/urandom", O_RDONLY, 0); - if (fd < 0) - return -1; - if (read(fd, (void *) state, len) < (ssize_t) len) { + if (fd >= 0) { + if (read(fd, (void *) state, len) == (ssize_t) len) + done = 1; close(fd); - return -1; } - close(fd); + + if (!done) { + struct timeval tv; + + gettimeofday(&tv, NULL); + srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec); + return; + } if (rand_type != TYPE_0) { fptr = &state[rand_sep]; rptr = &state[0]; } - return 0; } /* |