From owner-svn-src-all@freebsd.org Fri Dec 13 04:37:40 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id CCFDC1E1FE7; Fri, 13 Dec 2019 04:37:40 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47YyZX51xzz4Tt2; Fri, 13 Dec 2019 04:37:40 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id A7B0C6D4E; Fri, 13 Dec 2019 04:37:40 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id xBD4bent029953; Fri, 13 Dec 2019 04:37:40 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id xBD4bdYT029950; Fri, 13 Dec 2019 04:37:39 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201912130437.xBD4bdYT029950@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Fri, 13 Dec 2019 04:37:39 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r355693 - head/usr.bin/random X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: head/usr.bin/random X-SVN-Commit-Revision: 355693 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Dec 2019 04:37:40 -0000 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 #include #include +#include #include #include #include @@ -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 #include #include +#include #include #include #include @@ -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