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