xref: /freebsd/usr.bin/random/random.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 /*
2  * Copyright (c) 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Guy Harris at Network Appliance Corp.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <locale.h>
40 #include <stdbool.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
47 
48 #include "randomize_fd.h"
49 
50 static void usage(void) __dead2;
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	double denom;
56 	int ch, fd, random_exit, randomize_lines, random_type, ret,
57 	    unique_output, unbuffer_output;
58 	bool selected;
59 	char *ep;
60 	const char *filename;
61 
62 	denom = 0.;
63 	filename = "/dev/fd/0";
64 	random_type = RANDOM_TYPE_UNSET;
65 	random_exit = randomize_lines = unbuffer_output = 0;
66 	unique_output = 1;
67 
68 	(void)setlocale(LC_CTYPE, "");
69 
70 	while ((ch = getopt(argc, argv, "ef:hlruUw")) != -1)
71 		switch (ch) {
72 		case 'e':
73 			random_exit = 1;
74 			break;
75 		case 'f':
76 			randomize_lines = 1;
77 			if (strcmp(optarg, "-") != 0)
78 				filename = optarg;
79 			break;
80 		case 'l':
81 			randomize_lines = 1;
82 			random_type = RANDOM_TYPE_LINES;
83 			break;
84 		case 'r':
85 			unbuffer_output = 1;
86 			break;
87 		case 'u':
88 			randomize_lines = 1;
89 			unique_output = 1;
90 			break;
91 		case 'U':
92 			randomize_lines = 1;
93 			unique_output = 0;
94 			break;
95 		case 'w':
96 			randomize_lines = 1;
97 			random_type = RANDOM_TYPE_WORDS;
98 			break;
99 		default:
100 		case '?':
101 			usage();
102 			/* NOTREACHED */
103 		}
104 
105 	argc -= optind;
106 	argv += optind;
107 
108 	switch (argc) {
109 	case 0:
110 		denom = (randomize_lines ? 1. : 2.);
111 		break;
112 	case 1:
113 		errno = 0;
114 		denom = strtod(*argv, &ep);
115 		if (errno == ERANGE)
116 			err(1, "%s", *argv);
117 		if (denom < 1. || *ep != '\0')
118 			errx(1, "denominator is not valid.");
119 		if (random_exit && denom > 256.)
120 			errx(1, "denominator must be <= 256 for random exit.");
121 		break;
122 	default:
123 		usage();
124 		/* NOTREACHED */
125 	}
126 
127 	/*
128 	 * Act as a filter, randomly choosing lines of the standard input
129 	 * to write to the standard output.
130 	 */
131 	if (unbuffer_output)
132 		setbuf(stdout, NULL);
133 
134 	/*
135 	 * Act as a filter, randomizing lines read in from a given file
136 	 * descriptor and write the output to standard output.
137 	 */
138 	if (randomize_lines) {
139 		if ((fd = open(filename, O_RDONLY, 0)) < 0)
140 			err(1, "%s", filename);
141 		ret = randomize_fd(fd, random_type, unique_output, denom);
142 		if (!random_exit)
143 			return(ret);
144 	}
145 
146 	/* Compute a random exit status between 0 and denom - 1. */
147 	if (random_exit)
148 		return (arc4random_uniform(denom));
149 
150 	/*
151 	 * Filter stdin, selecting lines with probability 1/denom, one
152 	 * character at a time.
153 	 */
154 	do {
155 		selected = random_uniform_denom(denom);
156 		if (selected) {
157 			while ((ch = getchar()) != EOF) {
158 				putchar(ch);
159 				if (ch == '\n')
160 					break;
161 			}
162 		} else {
163 			while ((ch = getchar()) != EOF)
164 				if (ch == '\n')
165 					break;
166 		}
167 		if (ferror(stdout))
168 			err(2, "stdout");
169 	} while (ch != EOF);
170 	if (ferror(stdin))
171 		err(2, "stdin");
172 	exit (0);
173 }
174 
175 static void
176 usage(void)
177 {
178 
179 	fprintf(stderr, "usage: random [-elrUuw] [-f filename] [denominator]\n");
180 	exit(1);
181 }
182