Index: trunk/src/emx/bsd/libc/bsd.smak
===================================================================
--- trunk/src/emx/bsd/libc/bsd.smak	(revision 743)
+++ trunk/src/emx/bsd/libc/bsd.smak	(revision 743)
@@ -0,0 +1,23 @@
+# This is the top-level submakefile for building EMX C runtime library
+include comend.smak
+
+.MODULE	:= libbsd
+.MDESC	:= BSD LIBC stuff
+.MDEP   := emxomf
+
+.TARGET	:= libbsd_p.a
+.TKIND	:= aout prof
+.TSRC	:= bsd/libc/random.c
+.TCF    :=
+.INSDIR	= lib/
+.TKEEP	:= 1
+include mklib.smak
+include mkomflib.smak
+
+.TARGET	:= libbsd.a
+.TKIND	:= aout
+include mklib.smak
+include mkomflib.smak
+
+# Forget temporary variables
+include comend.smak
Index: trunk/src/emx/bsd/libc/random.c
===================================================================
--- trunk/src/emx/bsd/libc/random.c	(revision 742)
+++ trunk/src/emx/bsd/libc/random.c	(revision 743)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -33,11 +33,19 @@
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)random.c	5.9 (Berkeley) 2/23/91";
+static char sccsid[] = "@(#)random.c	8.2 (Berkeley) 5/19/95";
 #endif /* LIBC_SCCS and not lint */
-
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: src/lib/libc/stdlib/random.c,v 1.23 2003/08/10 17:49:55 ache Exp $");
+
+#ifndef __EMX__
+#include "namespace.h"
+#endif
+#include <sys/time.h>          /* for srandomdev() */
+#include <fcntl.h>             /* for srandomdev() */
 #include <stdio.h>
 #include <stdlib.h>
-#if defined (__EMX__)
-#include "emxbsd.h"
+#include <unistd.h>            /* for srandomdev() */
+#ifndef __EMX__
+#include "un-namespace.h"
 #endif
 
@@ -65,5 +73,5 @@
  * the zeroeth word of state information also has some other information
  * stored in it -- see setstate() for details).
- * 
+ *
  * The random number generation technique is a linear feedback shift register
  * approach, employing trinomials (since there are fewer terms to sum up that
@@ -77,7 +85,26 @@
  * the amount of state information has a vast influence on the period of the
  * generator.  Note: the deg*(2**deg - 1) is an approximation only good for
- * large deg, when the period of the shift register is the dominant factor.
+ * large deg, when the period of the shift is the dominant factor.
  * With deg equal to seven, the period is actually much longer than the
  * 7*(2**7 - 1) predicted by this formula.
+ *
+ * Modified 28 December 1994 by Jacob S. Rosenberg.
+ * The following changes have been made:
+ * All references to the type u_int have been changed to unsigned long.
+ * All references to type int have been changed to type long.  Other
+ * cleanups have been made as well.  A warning for both initstate and
+ * setstate has been inserted to the effect that on Sparc platforms
+ * the 'arg_state' variable must be forced to begin on word boundaries.
+ * This can be easily done by casting a long integer array to char *.
+ * The overall logic has been left STRICTLY alone.  This software was
+ * tested on both a VAX and Sun SpacsStation with exactly the same
+ * results.  The new version and the original give IDENTICAL results.
+ * The new version is somewhat faster than the original.  As the
+ * documentation says:  "By default, the package runs with 128 bytes of
+ * state information and generates far better random numbers than a linear
+ * congruential generator.  If the amount of state information is less than
+ * 32 bytes, a simple linear congruential R.N.G. is used."  For a buffer of
+ * 128 bytes, this new version runs about 19 percent faster and for a 16
+ * byte buffer it is about 5 percent faster.
  */
 
@@ -120,11 +147,17 @@
 #define	MAX_TYPES	5		/* max number of types above */
 
-static int degrees[MAX_TYPES] =	{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
-static int seps [MAX_TYPES] =	{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
+#ifdef  USE_WEAK_SEEDING
+#define NSHUFF 0
+#else   /* !USE_WEAK_SEEDING */
+#define NSHUFF 50       /* to drop some "seed -> 1st value" linearity */
+#endif  /* !USE_WEAK_SEEDING */
+
+static long degrees[MAX_TYPES] =	{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
+static long seps [MAX_TYPES] =	{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
 
 /*
  * Initially, everything is set up as if from:
  *
- *	initstate(1, &randtbl, 128);
+ *	initstate(1, randtbl, 128);
  *
  * Note that this initialization takes advantage of the fact that srandom()
@@ -139,4 +172,7 @@
 static long randtbl[DEG_3 + 1] = {
 	TYPE_3,
+#ifdef  USE_WEAK_SEEDING
+/* Historic implementation compatibility */
+/* The random sequences do not vary much with the seed */
 	0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5,
 	0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
@@ -145,4 +181,12 @@
 	0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b,
 	0x27fb47b9,
+#else   /* !USE_WEAK_SEEDING */
+	0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05,
+	0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454,
+	0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471,
+	0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1,
+	0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41,
+	0xf3bec5da
+#endif  /* !USE_WEAK_SEEDING */
 };
 
@@ -175,8 +219,43 @@
  */
 static long *state = &randtbl[1];
-static int rand_type = TYPE_3;
-static int rand_deg = DEG_3;
-static int rand_sep = SEP_3;
+static long rand_type = TYPE_3;
+static long rand_deg = DEG_3;
+static long rand_sep = SEP_3;
 static long *end_ptr = &randtbl[DEG_3 + 1];
+
+static inline long good_rand(long);
+
+static inline long good_rand (x)
+	long x;
+{
+#ifdef  USE_WEAK_SEEDING
+/*
+ * Historic implementation compatibility.
+ * The random sequences do not vary much with the seed,
+ * even with overflowing.
+ */
+	return (1103515245 * x + 12345);
+#else   /* !USE_WEAK_SEEDING */
+/*
+ * Compute x = (7^5 * x) mod (2^31 - 1)
+ * wihout overflowing 31 bits:
+ *      (2^31 - 1) = 127773 * (7^5) + 2836
+ * From "Random number generators: good ones are hard to find",
+ * Park and Miller, Communications of the ACM, vol. 31, no. 10,
+ * October 1988, p. 1195.
+ */
+	long hi, lo;
+
+	/* Can't be initialized with 0, so use another value. */
+	if (x == 0)
+		x = 123459876;
+	hi = x / 127773;
+	lo = x % 127773;
+	x = 16807 * lo - 2836 * hi;
+	if (x < 0)
+		x += 0x7fffffff;
+	return (x);
+#endif  /* !USE_WEAK_SEEDING */
+}
 
 /*
@@ -194,19 +273,64 @@
 void
 srandom(x)
-	u_int x;
+	unsigned long x;
 {
-	register int i, j;
-
+	long i, lim;
+
+	state[0] = x;
 	if (rand_type == TYPE_0)
-		state[0] = x;
+		lim = NSHUFF;
 	else {
-		j = 1;
-		state[0] = x;
 		for (i = 1; i < rand_deg; i++)
-			state[i] = 1103515245 * state[i - 1] + 12345;
+			state[i] = good_rand(state[i - 1]);
 		fptr = &state[rand_sep];
 		rptr = &state[0];
-		for (i = 0; i < 10 * rand_deg; i++)
-			(void)random();
+		lim = 10 * rand_deg;
+	}
+	for (i = 0; i < lim; i++)
+		(void)random();
+}
+
+/*
+ * srandomdev:
+ *
+ * Many programs choose the seed value in a totally predictable manner.
+ * This often causes problems.  We seed the generator using the much more
+ * secure random(4) interface.  Note that this particular seeding
+ * procedure can generate states which are impossible to reproduce by
+ * calling srandom() with any value, since the succeeding terms in the
+ * state buffer are no longer derived from the LC algorithm applied to
+ * a fixed seed.
+ */
+void
+srandomdev()
+{
+	int fd, done;
+	size_t len;
+
+	if (rand_type == TYPE_0)
+		len = sizeof state[0];
+	else
+		len = rand_deg * sizeof state[0];
+
+	done = 0;
+	fd = _open("/dev/random", O_RDONLY, 0);
+	if (fd >= 0) {
+		if (_read(fd, (void *) state, len) == (ssize_t) len)
+			done = 1;
+		_close(fd);
+	}
+
+	if (!done) {
+		struct timeval tv;
+		unsigned long junk;
+
+		gettimeofday(&tv, NULL);
+		srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk);
+		return;
+	}
+
+	if (rand_type != TYPE_0) {
+		fptr = &state[rand_sep];
+		rptr = &state[0];
 	}
 }
@@ -220,22 +344,27 @@
  * one we can and set things up for it.  srandom() is then called to
  * initialize the state information.
- * 
+ *
  * Note that on return from srandom(), we set state[-1] to be the type
  * multiplexed with the current value of the rear pointer; this is so
  * successive calls to initstate() won't lose this information and will be
  * able to restart with setstate().
- * 
+ *
  * Note: the first thing we do is save the current state, if any, just like
  * setstate() so that it doesn't matter when initstate is called.
  *
  * Returns a pointer to the old state.
+ *
+ * Note: The Sparc platform requires that arg_state begin on a long
+ * word boundary; otherwise a bus error will occur. Even so, lint will
+ * complain about mis-alignment, but you should disregard these messages.
  */
 char *
 initstate(seed, arg_state, n)
-	u_int seed;			/* seed for R.N.G. */
+	unsigned long seed;		/* seed for R.N.G. */
 	char *arg_state;		/* pointer to state array */
-	int n;				/* # bytes of state info */
+	long n;				/* # bytes of state info */
 {
-	register char *ostate = (char *)(&state[-1]);
+	char *ostate = (char *)(&state[-1]);
+	long *long_arg_state = (long *) arg_state;
 
 	if (rand_type == TYPE_0)
@@ -245,5 +374,5 @@
 	if (n < BREAK_0) {
 		(void)fprintf(stderr,
-		    "random: not enough state (%d bytes); ignored.\n", n);
+		    "random: not enough state (%ld bytes); ignored.\n", n);
 		return(0);
 	}
@@ -269,11 +398,11 @@
 		rand_sep = SEP_4;
 	}
-	state = &(((long *)arg_state)[1]);	/* first location */
+	state = (long *) (long_arg_state + 1); /* first location */
 	end_ptr = &state[rand_deg];	/* must set end_ptr before srandom */
 	srandom(seed);
 	if (rand_type == TYPE_0)
-		state[-1] = rand_type;
+		long_arg_state[0] = rand_type;
 	else
-		state[-1] = MAX_TYPES*(rptr - state) + rand_type;
+		long_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type;
 	return(ostate);
 }
@@ -293,12 +422,16 @@
  *
  * Returns a pointer to the old state information.
+ *
+ * Note: The Sparc platform requires that arg_state begin on a long
+ * word boundary; otherwise a bus error will occur. Even so, lint will
+ * complain about mis-alignment, but you should disregard these messages.
  */
 char *
 setstate(arg_state)
-	char *arg_state;
+	char *arg_state;		/* pointer to state array */
 {
-	register long *new_state = (long *)arg_state;
-	register int type = new_state[0] % MAX_TYPES;
-	register int rear = new_state[0] / MAX_TYPES;
+	long *new_state = (long *) arg_state;
+	long type = new_state[0] % MAX_TYPES;
+	long rear = new_state[0] / MAX_TYPES;
 	char *ostate = (char *)(&state[-1]);
 
@@ -321,5 +454,5 @@
 		    "random: state info corrupted; not changed.\n");
 	}
-	state = &new_state[1];
+	state = (long *) (new_state + 1);
 	if (rand_type != TYPE_0) {
 		rptr = &state[rear];
@@ -351,15 +484,25 @@
 {
 	long i;
-
-	if (rand_type == TYPE_0)
-		i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff;
-	else {
-		*fptr += *rptr;
-		i = (*fptr >> 1) & 0x7fffffff;	/* chucking least random bit */
-		if (++fptr >= end_ptr) {
-			fptr = state;
-			++rptr;
-		} else if (++rptr >= end_ptr)
-			rptr = state;
+	long *f, *r;
+
+	if (rand_type == TYPE_0) {
+		i = state[0];
+		state[0] = i = (good_rand(i)) & 0x7fffffff;
+	} else {
+		/*
+		 * Use local variables rather than static variables for speed.
+		 */
+		f = fptr; r = rptr;
+		*f += *r;
+		i = (*f >> 1) & 0x7fffffff;	/* chucking least random bit */
+		if (++f >= end_ptr) {
+			f = state;
+			++r;
+		}
+		else if (++r >= end_ptr) {
+			r = state;
+		}
+
+		fptr = f; rptr = r;
 	}
 	return(i);
Index: trunk/src/emx/include/stdlib.h
===================================================================
--- trunk/src/emx/include/stdlib.h	(revision 742)
+++ trunk/src/emx/include/stdlib.h	(revision 743)
@@ -149,5 +149,5 @@
 extern char **environ;
 
-extern __const__ char * __const__ sys_errlist[]; 
+extern __const__ char * __const__ sys_errlist[];
 extern __const__ int sys_nerr;
 
@@ -174,10 +174,13 @@
 long ulimit (int, ...);
 
+/* BSD - start */
 int heapsort (void *, size_t, size_t,
     int (*)(__const__ void *, __const__ void *));       /* BSD */
-char *initstate (unsigned, char *, int);                /* BSD */
-long random (void);                                     /* BSD */
-char *setstate (char *);                                /* BSD */
-void srandom (unsigned);                                /* BSD */
+char	*initstate(unsigned long /* XSI requires u_int */, char *, long);
+char	*setstate(/* const */ char *);
+long	 random(void);
+void	 srandom(unsigned long);
+void	 srandomdev(void);
+/* BSD - end */
 
 char *itoa (int, char *, int);
@@ -197,5 +200,5 @@
 
 extern char **_environ;
-extern __const__ char * __const__ _sys_errlist[]; 
+extern __const__ char * __const__ _sys_errlist[];
 extern __const__ int _sys_nerr;
 
