xref: /openbsd/games/random/random.c (revision 404b540a)
1 /*	$OpenBSD: random.c,v 1.11 2008/04/13 00:22:16 djm Exp $	*/
2 /*	$NetBSD: random.c,v 1.3 1995/04/22 07:44:05 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Guy Harris at Network Appliance Corp.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1994\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)random.c	8.5 (Berkeley) 4/5/94";
45 #else
46 static char rcsid[] = "$OpenBSD: random.c,v 1.11 2008/04/13 00:22:16 djm Exp $";
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/types.h>
51 
52 #include <dev/rndvar.h>
53 
54 #include <err.h>
55 #include <errno.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <time.h>
59 #include <unistd.h>
60 #include <limits.h>
61 
62 void usage(void);
63 
64 int
65 main(int argc, char *argv[])
66 {
67 	double denom;
68 	int ch, random_exit, selected, unbuffer_output;
69 	char *ep;
70 
71 	random_exit = unbuffer_output = 0;
72 	while ((ch = getopt(argc, argv, "erh")) != -1)
73 		switch (ch) {
74 		case 'e':
75 			random_exit = 1;
76 			break;
77 		case 'r':
78 			unbuffer_output = 1;
79 			break;
80 		default:
81 		case '?': case 'h':
82 			usage();
83 			/* NOTREACHED */
84 		}
85 
86 	argc -= optind;
87 	argv += optind;
88 
89 	switch (argc) {
90 	case 0:
91 		denom = 2;
92 		break;
93 	case 1:
94 		errno = 0;
95 		denom = strtod(*argv, &ep);
96 		if (errno == ERANGE)
97 			err(1, "%s", *argv);
98 		if (denom == 0 || *ep != '\0')
99 			errx(1, "denominator is not valid.");
100 		break;
101 	default:
102 		usage();
103 		/* NOTREACHED */
104 	}
105 
106 	/* Compute a random exit status between 0 and denom - 1. */
107 	if (random_exit)
108 		return (arc4random_uniform(denom));
109 
110 	/*
111 	 * Act as a filter, randomly choosing lines of the standard input
112 	 * to write to the standard output.
113 	 */
114 	if (unbuffer_output)
115 		setbuf(stdout, NULL);
116 
117 	/*
118 	 * Select whether to print the first line.  (Prime the pump.)
119 	 * We find a random number between 0 and denom - 1 and, if it's
120 	 * 0 (which has a 1 / denom chance of being true), we select the
121 	 * line.
122 	 */
123 	selected = arc4random_uniform(denom) == 0;
124 	while ((ch = getchar()) != EOF) {
125 		if (selected)
126 			(void)putchar(ch);
127 		if (ch == '\n') {
128 			/* End of that line.  See if we got an error. */
129 			if (ferror(stdout))
130 				err(2, "stdout");
131 
132 			/* Now see if the next line is to be printed. */
133 			selected = arc4random_uniform(denom) == 0;
134 		}
135 	}
136 	if (ferror(stdin))
137 		err(2, "stdin");
138 	exit (0);
139 }
140 
141 void
142 usage(void)
143 {
144 
145 	(void)fprintf(stderr, "usage: random [-er] [denominator]\n");
146 	exit(1);
147 }
148