Deleted Added
full compact
random.c (157758) random.c (181409)
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

--- 32 unchanged lines hidden (view full) ---

41 The Regents of the University of California. All rights reserved.\n";
42#endif /* not lint */
43
44#ifndef lint
45static const char sccsid[] = "@(#)random.c 8.5 (Berkeley) 4/5/94";
46#endif /* not lint */
47#endif
48#include <sys/cdefs.h>
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

--- 32 unchanged lines hidden (view full) ---

41 The Regents of the University of California. All rights reserved.\n";
42#endif /* not lint */
43
44#ifndef lint
45static const char sccsid[] = "@(#)random.c 8.5 (Berkeley) 4/5/94";
46#endif /* not lint */
47#endif
48#include <sys/cdefs.h>
49__FBSDID("$FreeBSD: head/games/random/random.c 157758 2006-04-14 17:32:27Z ache $");
49__FBSDID("$FreeBSD: head/games/random/random.c 181409 2008-08-08 01:02:30Z ache $");
50
51#include <sys/types.h>
52
53#include <err.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <limits.h>
57#include <locale.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <time.h>
62#include <unistd.h>
63
64#include "randomize_fd.h"
65
50
51#include <sys/types.h>
52
53#include <err.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <limits.h>
57#include <locale.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <time.h>
62#include <unistd.h>
63
64#include "randomize_fd.h"
65
66/*
67 * The random() function is defined to return values between 0 and
68 * 2^31 - 1 inclusive in random(3).
69 */
70#define RANDOM_MAX 0x7fffffffL
71
72static void usage(void);
73
74int
75main(int argc, char *argv[])
76{
77 double denom;
78 int ch, fd, random_exit, randomize_lines, random_type, ret,
79 selected, unique_output, unbuffer_output;

--- 52 unchanged lines hidden (view full) ---

132 break;
133 case 1:
134 errno = 0;
135 denom = strtod(*argv, &ep);
136 if (errno == ERANGE)
137 err(1, "%s", *argv);
138 if (denom <= 0 || *ep != '\0')
139 errx(1, "denominator is not valid.");
66static void usage(void);
67
68int
69main(int argc, char *argv[])
70{
71 double denom;
72 int ch, fd, random_exit, randomize_lines, random_type, ret,
73 selected, unique_output, unbuffer_output;

--- 52 unchanged lines hidden (view full) ---

126 break;
127 case 1:
128 errno = 0;
129 denom = strtod(*argv, &ep);
130 if (errno == ERANGE)
131 err(1, "%s", *argv);
132 if (denom <= 0 || *ep != '\0')
133 errx(1, "denominator is not valid.");
140 if (random_exit && denom > 255)
141 errx(1, "denominator must be <= 255 for random exit.");
134 if (random_exit && denom > 256)
135 errx(1, "denominator must be <= 256 for random exit.");
142 break;
143 default:
144 usage();
145 /* NOTREACHED */
146 }
147
148 srandomdev();
149

--- 13 unchanged lines hidden (view full) ---

163 err(1, "%s", filename);
164 ret = randomize_fd(fd, random_type, unique_output, denom);
165 if (!random_exit)
166 return(ret);
167 }
168
169 /* Compute a random exit status between 0 and denom - 1. */
170 if (random_exit)
136 break;
137 default:
138 usage();
139 /* NOTREACHED */
140 }
141
142 srandomdev();
143

--- 13 unchanged lines hidden (view full) ---

157 err(1, "%s", filename);
158 ret = randomize_fd(fd, random_type, unique_output, denom);
159 if (!random_exit)
160 return(ret);
161 }
162
163 /* Compute a random exit status between 0 and denom - 1. */
164 if (random_exit)
171 return (int)((denom * random()) / RANDOM_MAX);
165 return (int)(denom * random() / ((double)RAND_MAX + 1));
172
173 /*
174 * Select whether to print the first line. (Prime the pump.)
175 * We find a random number between 0 and denom - 1 and, if it's
176 * 0 (which has a 1 / denom chance of being true), we select the
177 * line.
178 */
166
167 /*
168 * Select whether to print the first line. (Prime the pump.)
169 * We find a random number between 0 and denom - 1 and, if it's
170 * 0 (which has a 1 / denom chance of being true), we select the
171 * line.
172 */
179 selected = (int)(denom * random() / RANDOM_MAX) == 0;
173 selected = (int)(denom * random() / ((double)RAND_MAX + 1)) == 0;
180 while ((ch = getchar()) != EOF) {
181 if (selected)
182 (void)putchar(ch);
183 if (ch == '\n') {
184 /* End of that line. See if we got an error. */
185 if (ferror(stdout))
186 err(2, "stdout");
187
188 /* Now see if the next line is to be printed. */
174 while ((ch = getchar()) != EOF) {
175 if (selected)
176 (void)putchar(ch);
177 if (ch == '\n') {
178 /* End of that line. See if we got an error. */
179 if (ferror(stdout))
180 err(2, "stdout");
181
182 /* Now see if the next line is to be printed. */
189 selected = (int)(denom * random() / RANDOM_MAX) == 0;
183 selected = (int)(denom * random() / ((double)RAND_MAX + 1)) == 0;
190 }
191 }
192 if (ferror(stdin))
193 err(2, "stdin");
194 exit (0);
195}
196
197static void
198usage(void)
199{
200
201 fprintf(stderr, "usage: random [-elrUuw] [-f filename] [denominator]\n");
202 exit(1);
203}
184 }
185 }
186 if (ferror(stdin))
187 err(2, "stdin");
188 exit (0);
189}
190
191static void
192usage(void)
193{
194
195 fprintf(stderr, "usage: random [-elrUuw] [-f filename] [denominator]\n");
196 exit(1);
197}