Instead of copying fallback code over and over in each program,

implement (better) falback code inside srandomdev() itself.
Change return type from int to void (binary compatibility surprisely
achieved). Userland code will be changed soon.
This commit is contained in:
Andrey A. Chernov 1997-06-14 00:14:29 +00:00
parent 854d14213e
commit 96c31b2618
3 changed files with 18 additions and 16 deletions

View File

@ -160,7 +160,7 @@ long random __P((void));
char *realpath __P((const char *, char resolved_path[])); char *realpath __P((const char *, char resolved_path[]));
char *setstate __P((char *)); char *setstate __P((char *));
void srandom __P((unsigned long)); void srandom __P((unsigned long));
int srandomdev __P((void)); void srandomdev __P((void));
char *user_from_uid __P((unsigned long, int)); char *user_from_uid __P((unsigned long, int));
#ifndef __STRICT_ANSI__ #ifndef __STRICT_ANSI__
long long long long

View File

@ -47,7 +47,7 @@
.Fn random void .Fn random void
.Ft void .Ft void
.Fn srandom "unsigned long seed" .Fn srandom "unsigned long seed"
.Ft int .Ft void
.Fn srandomdev void .Fn srandomdev void
.Ft char * .Ft char *
.Fn initstate "unsigned long seed" "char *state" "long n" .Fn initstate "unsigned long seed" "char *state" "long n"
@ -108,11 +108,6 @@ calling
with any value, since the succeeding terms in the with any value, since the succeeding terms in the
state buffer are no longer derived from the LC algorithm applied to state buffer are no longer derived from the LC algorithm applied to
a fixed seed. a fixed seed.
If successful
.Fn srandomdev
returns 0. It returns -1 on failure, and sets
.Va errno
to indicate the error.
.Pp .Pp
The The
.Fn initstate .Fn initstate

View File

@ -30,7 +30,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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"; static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <sys/time.h> /* for srandomdev() */
#include <fcntl.h> /* for srandomdev() */ #include <fcntl.h> /* for srandomdev() */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -285,10 +286,10 @@ srandom(x)
* state buffer are no longer derived from the LC algorithm applied to * state buffer are no longer derived from the LC algorithm applied to
* a fixed seed. * a fixed seed.
*/ */
int void
srandomdev() srandomdev()
{ {
int fd; int fd, done;
size_t len; size_t len;
if (rand_type == TYPE_0) if (rand_type == TYPE_0)
@ -296,20 +297,26 @@ srandomdev()
else else
len = rand_deg * sizeof state[0]; len = rand_deg * sizeof state[0];
done = 0;
fd = open("/dev/urandom", O_RDONLY, 0); fd = open("/dev/urandom", O_RDONLY, 0);
if (fd < 0) if (fd >= 0) {
return -1; if (read(fd, (void *) state, len) == (ssize_t) len)
if (read(fd, (void *) state, len) < (ssize_t) len) { done = 1;
close(fd); 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) { if (rand_type != TYPE_0) {
fptr = &state[rand_sep]; fptr = &state[rand_sep];
rptr = &state[0]; rptr = &state[0];
} }
return 0;
} }
/* /*