xref: /freebsd/usr.bin/random/random.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
16ae1554aSColin Percival /*
26ae1554aSColin Percival  * Copyright (c) 1994
36ae1554aSColin Percival  *	The Regents of the University of California.  All rights reserved.
46ae1554aSColin Percival  *
56ae1554aSColin Percival  * This code is derived from software contributed to Berkeley by
66ae1554aSColin Percival  * Guy Harris at Network Appliance Corp.
76ae1554aSColin Percival  *
86ae1554aSColin Percival  * Redistribution and use in source and binary forms, with or without
96ae1554aSColin Percival  * modification, are permitted provided that the following conditions
106ae1554aSColin Percival  * are met:
116ae1554aSColin Percival  * 1. Redistributions of source code must retain the above copyright
126ae1554aSColin Percival  *    notice, this list of conditions and the following disclaimer.
136ae1554aSColin Percival  * 2. Redistributions in binary form must reproduce the above copyright
146ae1554aSColin Percival  *    notice, this list of conditions and the following disclaimer in the
156ae1554aSColin Percival  *    documentation and/or other materials provided with the distribution.
166ae1554aSColin Percival  * 3. Neither the name of the University nor the names of its contributors
176ae1554aSColin Percival  *    may be used to endorse or promote products derived from this software
186ae1554aSColin Percival  *    without specific prior written permission.
196ae1554aSColin Percival  *
206ae1554aSColin Percival  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
216ae1554aSColin Percival  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226ae1554aSColin Percival  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236ae1554aSColin Percival  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
246ae1554aSColin Percival  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256ae1554aSColin Percival  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266ae1554aSColin Percival  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276ae1554aSColin Percival  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286ae1554aSColin Percival  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296ae1554aSColin Percival  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306ae1554aSColin Percival  * SUCH DAMAGE.
316ae1554aSColin Percival  */
326ae1554aSColin Percival 
336ae1554aSColin Percival #include <sys/types.h>
346ae1554aSColin Percival 
356ae1554aSColin Percival #include <err.h>
366ae1554aSColin Percival #include <errno.h>
376ae1554aSColin Percival #include <fcntl.h>
386ae1554aSColin Percival #include <limits.h>
396ae1554aSColin Percival #include <locale.h>
4006257a71SConrad Meyer #include <stdbool.h>
41c7b8411cSConrad Meyer #include <stdint.h>
426ae1554aSColin Percival #include <stdio.h>
436ae1554aSColin Percival #include <stdlib.h>
446ae1554aSColin Percival #include <string.h>
456ae1554aSColin Percival #include <time.h>
466ae1554aSColin Percival #include <unistd.h>
476ae1554aSColin Percival 
486ae1554aSColin Percival #include "randomize_fd.h"
496ae1554aSColin Percival 
50*1a7ac2bdSAlfonso Gregory static void usage(void) __dead2;
516ae1554aSColin Percival 
526ae1554aSColin Percival int
main(int argc,char * argv[])536ae1554aSColin Percival main(int argc, char *argv[])
546ae1554aSColin Percival {
556ae1554aSColin Percival 	double denom;
566ae1554aSColin Percival 	int ch, fd, random_exit, randomize_lines, random_type, ret,
5706257a71SConrad Meyer 	    unique_output, unbuffer_output;
5806257a71SConrad Meyer 	bool selected;
596ae1554aSColin Percival 	char *ep;
606ae1554aSColin Percival 	const char *filename;
616ae1554aSColin Percival 
6206257a71SConrad Meyer 	denom = 0.;
636ae1554aSColin Percival 	filename = "/dev/fd/0";
646ae1554aSColin Percival 	random_type = RANDOM_TYPE_UNSET;
656ae1554aSColin Percival 	random_exit = randomize_lines = unbuffer_output = 0;
666ae1554aSColin Percival 	unique_output = 1;
676ae1554aSColin Percival 
686ae1554aSColin Percival 	(void)setlocale(LC_CTYPE, "");
696ae1554aSColin Percival 
706ae1554aSColin Percival 	while ((ch = getopt(argc, argv, "ef:hlruUw")) != -1)
716ae1554aSColin Percival 		switch (ch) {
726ae1554aSColin Percival 		case 'e':
736ae1554aSColin Percival 			random_exit = 1;
746ae1554aSColin Percival 			break;
756ae1554aSColin Percival 		case 'f':
766ae1554aSColin Percival 			randomize_lines = 1;
776ae1554aSColin Percival 			if (strcmp(optarg, "-") != 0)
786ae1554aSColin Percival 				filename = optarg;
796ae1554aSColin Percival 			break;
806ae1554aSColin Percival 		case 'l':
816ae1554aSColin Percival 			randomize_lines = 1;
826ae1554aSColin Percival 			random_type = RANDOM_TYPE_LINES;
836ae1554aSColin Percival 			break;
846ae1554aSColin Percival 		case 'r':
856ae1554aSColin Percival 			unbuffer_output = 1;
866ae1554aSColin Percival 			break;
876ae1554aSColin Percival 		case 'u':
886ae1554aSColin Percival 			randomize_lines = 1;
896ae1554aSColin Percival 			unique_output = 1;
906ae1554aSColin Percival 			break;
916ae1554aSColin Percival 		case 'U':
926ae1554aSColin Percival 			randomize_lines = 1;
936ae1554aSColin Percival 			unique_output = 0;
946ae1554aSColin Percival 			break;
956ae1554aSColin Percival 		case 'w':
966ae1554aSColin Percival 			randomize_lines = 1;
976ae1554aSColin Percival 			random_type = RANDOM_TYPE_WORDS;
986ae1554aSColin Percival 			break;
996ae1554aSColin Percival 		default:
1006ae1554aSColin Percival 		case '?':
1016ae1554aSColin Percival 			usage();
1026ae1554aSColin Percival 			/* NOTREACHED */
1036ae1554aSColin Percival 		}
1046ae1554aSColin Percival 
1056ae1554aSColin Percival 	argc -= optind;
1066ae1554aSColin Percival 	argv += optind;
1076ae1554aSColin Percival 
1086ae1554aSColin Percival 	switch (argc) {
1096ae1554aSColin Percival 	case 0:
11006257a71SConrad Meyer 		denom = (randomize_lines ? 1. : 2.);
1116ae1554aSColin Percival 		break;
1126ae1554aSColin Percival 	case 1:
1136ae1554aSColin Percival 		errno = 0;
1146ae1554aSColin Percival 		denom = strtod(*argv, &ep);
1156ae1554aSColin Percival 		if (errno == ERANGE)
1166ae1554aSColin Percival 			err(1, "%s", *argv);
11706257a71SConrad Meyer 		if (denom < 1. || *ep != '\0')
1186ae1554aSColin Percival 			errx(1, "denominator is not valid.");
11906257a71SConrad Meyer 		if (random_exit && denom > 256.)
1206ae1554aSColin Percival 			errx(1, "denominator must be <= 256 for random exit.");
1216ae1554aSColin Percival 		break;
1226ae1554aSColin Percival 	default:
1236ae1554aSColin Percival 		usage();
1246ae1554aSColin Percival 		/* NOTREACHED */
1256ae1554aSColin Percival 	}
1266ae1554aSColin Percival 
1276ae1554aSColin Percival 	/*
1286ae1554aSColin Percival 	 * Act as a filter, randomly choosing lines of the standard input
1296ae1554aSColin Percival 	 * to write to the standard output.
1306ae1554aSColin Percival 	 */
1316ae1554aSColin Percival 	if (unbuffer_output)
1326ae1554aSColin Percival 		setbuf(stdout, NULL);
1336ae1554aSColin Percival 
1346ae1554aSColin Percival 	/*
1356ae1554aSColin Percival 	 * Act as a filter, randomizing lines read in from a given file
1366ae1554aSColin Percival 	 * descriptor and write the output to standard output.
1376ae1554aSColin Percival 	 */
1386ae1554aSColin Percival 	if (randomize_lines) {
1396ae1554aSColin Percival 		if ((fd = open(filename, O_RDONLY, 0)) < 0)
1406ae1554aSColin Percival 			err(1, "%s", filename);
1416ae1554aSColin Percival 		ret = randomize_fd(fd, random_type, unique_output, denom);
1426ae1554aSColin Percival 		if (!random_exit)
1436ae1554aSColin Percival 			return(ret);
1446ae1554aSColin Percival 	}
1456ae1554aSColin Percival 
1466ae1554aSColin Percival 	/* Compute a random exit status between 0 and denom - 1. */
1476ae1554aSColin Percival 	if (random_exit)
148c7b8411cSConrad Meyer 		return (arc4random_uniform(denom));
1496ae1554aSColin Percival 
1506ae1554aSColin Percival 	/*
15106257a71SConrad Meyer 	 * Filter stdin, selecting lines with probability 1/denom, one
15206257a71SConrad Meyer 	 * character at a time.
1536ae1554aSColin Percival 	 */
15406257a71SConrad Meyer 	do {
15506257a71SConrad Meyer 		selected = random_uniform_denom(denom);
15606257a71SConrad Meyer 		if (selected) {
1576ae1554aSColin Percival 			while ((ch = getchar()) != EOF) {
15806257a71SConrad Meyer 				putchar(ch);
15906257a71SConrad Meyer 				if (ch == '\n')
16006257a71SConrad Meyer 					break;
16106257a71SConrad Meyer 			}
16206257a71SConrad Meyer 		} else {
16306257a71SConrad Meyer 			while ((ch = getchar()) != EOF)
16406257a71SConrad Meyer 				if (ch == '\n')
16506257a71SConrad Meyer 					break;
16606257a71SConrad Meyer 		}
1676ae1554aSColin Percival 		if (ferror(stdout))
1686ae1554aSColin Percival 			err(2, "stdout");
16906257a71SConrad Meyer 	} while (ch != EOF);
1706ae1554aSColin Percival 	if (ferror(stdin))
1716ae1554aSColin Percival 		err(2, "stdin");
1726ae1554aSColin Percival 	exit (0);
1736ae1554aSColin Percival }
1746ae1554aSColin Percival 
1756ae1554aSColin Percival static void
usage(void)1766ae1554aSColin Percival usage(void)
1776ae1554aSColin Percival {
1786ae1554aSColin Percival 
1796ae1554aSColin Percival 	fprintf(stderr, "usage: random [-elrUuw] [-f filename] [denominator]\n");
1806ae1554aSColin Percival 	exit(1);
1816ae1554aSColin Percival }
182