svn commit: r355693 - head/usr.bin/random
Conrad Meyer
cem at FreeBSD.org
Fri Dec 13 04:37:40 UTC 2019
Author: cem
Date: Fri Dec 13 04:37:39 2019
New Revision: 355693
URL: https://svnweb.freebsd.org/changeset/base/355693
Log:
random(6): produce random results
This program is trash and there's no reason to keep it in base. But as long as
we're shipping a silly program named 'random', let's actually make it random.
Modified:
head/usr.bin/random/random.6
head/usr.bin/random/random.c
head/usr.bin/random/randomize_fd.c
head/usr.bin/random/randomize_fd.h
Modified: head/usr.bin/random/random.6
==============================================================================
--- head/usr.bin/random/random.6 Fri Dec 13 04:12:13 2019 (r355692)
+++ head/usr.bin/random/random.6 Fri Dec 13 04:37:39 2019 (r355693)
@@ -28,7 +28,7 @@
.\" @(#)random.6 8.2 (Berkeley) 3/31/94
.\" $FreeBSD$
.\"
-.Dd February 8, 2003
+.Dd December 12, 2019
.Dt RANDOM 6
.Os
.Sh NAME
@@ -62,9 +62,7 @@ space characters as determined by
The default
.Ar denominator
for this mode of operation is 1, which gives each line a chance to be
-displayed, but in a
-.Xr random 3
-order.
+displayed, but in a random order.
.Pp
The options are as follows:
.Bl -tag -width Ds
@@ -112,7 +110,6 @@ Randomize words separated by
instead of newlines.
.El
.Sh SEE ALSO
-.Xr random 3 ,
.Xr fortune 6
.Sh HISTORY
The
Modified: head/usr.bin/random/random.c
==============================================================================
--- head/usr.bin/random/random.c Fri Dec 13 04:12:13 2019 (r355692)
+++ head/usr.bin/random/random.c Fri Dec 13 04:37:39 2019 (r355693)
@@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
#include <fcntl.h>
#include <limits.h>
#include <locale.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -135,8 +136,6 @@ main(int argc, char *argv[])
/* NOTREACHED */
}
- srandomdev();
-
/*
* Act as a filter, randomly choosing lines of the standard input
* to write to the standard output.
@@ -158,7 +157,7 @@ main(int argc, char *argv[])
/* Compute a random exit status between 0 and denom - 1. */
if (random_exit)
- return (int)(denom * random() / RANDOM_MAX_PLUS1);
+ return (arc4random_uniform(denom));
/*
* Select whether to print the first line. (Prime the pump.)
@@ -166,7 +165,7 @@ main(int argc, char *argv[])
* 0 (which has a 1 / denom chance of being true), we select the
* line.
*/
- selected = (int)(denom * random() / RANDOM_MAX_PLUS1) == 0;
+ selected = (arc4random_uniform(denom) == 0);
while ((ch = getchar()) != EOF) {
if (selected)
(void)putchar(ch);
@@ -176,8 +175,7 @@ main(int argc, char *argv[])
err(2, "stdout");
/* Now see if the next line is to be printed. */
- selected = (int)(denom * random() /
- RANDOM_MAX_PLUS1) == 0;
+ selected = (arc4random_uniform(denom) == 0);
}
}
if (ferror(stdin))
Modified: head/usr.bin/random/randomize_fd.c
==============================================================================
--- head/usr.bin/random/randomize_fd.c Fri Dec 13 04:12:13 2019 (r355692)
+++ head/usr.bin/random/randomize_fd.c Fri Dec 13 04:37:39 2019 (r355693)
@@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <errno.h>
#include <stdlib.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -175,7 +176,7 @@ randomize_fd(int fd, int type, int unique, double deno
(type == RANDOM_TYPE_WORDS && isspace(buf[i])) ||
(eof && i == buflen - 1)) {
make_token:
- if (numnode == RANDOM_MAX_PLUS1) {
+ if (numnode == UINT32_MAX - 1) {
errno = EFBIG;
err(1, "too many delimiters");
}
@@ -210,15 +211,14 @@ make_token:
free(buf);
for (i = numnode; i > 0; i--) {
- selected = random() % numnode;
+ selected = arc4random_uniform(numnode + 1);
for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
if (j == selected) {
if (n->cp == NULL)
break;
- if ((int)(denom * random() /
- RANDOM_MAX_PLUS1) == 0) {
+ if (arc4random_uniform(denom) == 0) {
ret = printf("%.*s",
(int)n->len - 1, n->cp);
if (ret < 0)
Modified: head/usr.bin/random/randomize_fd.h
==============================================================================
--- head/usr.bin/random/randomize_fd.h Fri Dec 13 04:12:13 2019 (r355692)
+++ head/usr.bin/random/randomize_fd.h Fri Dec 13 04:37:39 2019 (r355693)
@@ -29,12 +29,6 @@
#ifndef __RANDOMIZE_FD__
#define __RANDOMIZE_FD__
-/*
- * The random() function is defined to return values between 0 and
- * 2^31 - 1 inclusive in random(3).
- */
-#define RANDOM_MAX_PLUS1 0x80000000UL
-
#define RANDOM_TYPE_UNSET 0
#define RANDOM_TYPE_LINES 1
#define RANDOM_TYPE_WORDS 2
More information about the svn-src-all
mailing list