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