shuffle.c revision 1.8 1 1.8 cgd /* $NetBSD: shuffle.c,v 1.8 2001/02/19 23:03:51 cgd Exp $ */
2 1.1 perry
3 1.1 perry /*
4 1.1 perry * Copyright (c) 1998
5 1.1 perry * Perry E. Metzger. All rights reserved.
6 1.1 perry *
7 1.1 perry * Redistribution and use in source and binary forms, with or without
8 1.1 perry * modification, are permitted provided that the following conditions
9 1.1 perry * are met:
10 1.1 perry * 1. Redistributions of source code must retain the above copyright
11 1.1 perry * notice, this list of conditions and the following disclaimer.
12 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 perry * notice, this list of conditions and the following disclaimer in the
14 1.1 perry * documentation and/or other materials provided with the distribution.
15 1.1 perry * 3. All advertising materials mentioning features or use of this software
16 1.1 perry * must display the following acknowledgment:
17 1.1 perry * This product includes software developed for the NetBSD Project
18 1.1 perry * by Perry E. Metzger.
19 1.1 perry * 4. The name of the author may not be used to endorse or promote products
20 1.1 perry * derived from this software without specific prior written permission.
21 1.1 perry *
22 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 perry * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 perry * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 perry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 perry * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 perry * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 perry * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 perry * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 perry * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 perry */
33 1.1 perry
34 1.1 perry #include <sys/cdefs.h>
35 1.1 perry #ifndef lint
36 1.8 cgd __RCSID("$NetBSD: shuffle.c,v 1.8 2001/02/19 23:03:51 cgd Exp $");
37 1.1 perry #endif /* not lint */
38 1.1 perry
39 1.1 perry #include <err.h>
40 1.1 perry #include <stdio.h>
41 1.1 perry #include <stdlib.h>
42 1.1 perry #include <string.h>
43 1.1 perry #include <unistd.h>
44 1.3 christos #include <errno.h>
45 1.1 perry #include <sys/time.h>
46 1.1 perry
47 1.3 christos static void enomem __P((void));
48 1.3 christos static void *emalloc __P((size_t));
49 1.3 christos static void *erealloc __P((void *, size_t));
50 1.3 christos
51 1.3 christos static size_t *get_shuffle __P((size_t));
52 1.3 christos static void usage __P((void));
53 1.3 christos static void get_lines __P((const char *, char ***, size_t *));
54 1.3 christos static size_t get_number __P((const char *, int));
55 1.2 perry
56 1.3 christos int main __P((int, char *[]));
57 1.1 perry
58 1.1 perry /*
59 1.1 perry * enomem --
60 1.1 perry * die when out of memory.
61 1.1 perry */
62 1.3 christos static void
63 1.3 christos enomem()
64 1.1 perry {
65 1.1 perry errx(2, "Cannot allocate memory.");
66 1.1 perry }
67 1.1 perry
68 1.1 perry /*
69 1.1 perry * emalloc --
70 1.1 perry * malloc, but die on error.
71 1.1 perry */
72 1.3 christos static void *
73 1.3 christos emalloc(len)
74 1.3 christos size_t len;
75 1.1 perry {
76 1.1 perry void *p;
77 1.1 perry
78 1.1 perry if ((p = malloc(len)) == NULL)
79 1.1 perry enomem();
80 1.3 christos return p;
81 1.1 perry }
82 1.1 perry
83 1.1 perry /*
84 1.1 perry * erealloc --
85 1.1 perry * realloc, but die on error.
86 1.1 perry */
87 1.1 perry void *
88 1.3 christos erealloc(ptr, size)
89 1.3 christos void *ptr;
90 1.3 christos size_t size;
91 1.1 perry {
92 1.1 perry if ((ptr = realloc(ptr, size)) == NULL)
93 1.1 perry enomem();
94 1.3 christos return ptr;
95 1.1 perry }
96 1.1 perry
97 1.3 christos /*
98 1.3 christos * get_shuffle --
99 1.3 christos * Construct a random shuffle array of t elements
100 1.3 christos */
101 1.3 christos static size_t *
102 1.3 christos get_shuffle(t)
103 1.3 christos size_t t;
104 1.1 perry {
105 1.3 christos size_t *shuffle;
106 1.3 christos size_t i, j, k, temp;
107 1.1 perry
108 1.3 christos shuffle = emalloc(t * sizeof(size_t));
109 1.1 perry
110 1.1 perry for (i = 0; i < t; i++)
111 1.1 perry shuffle[i] = i;
112 1.1 perry
113 1.1 perry /*
114 1.1 perry * This algorithm taken from Knuth, Seminumerical Algorithms,
115 1.1 perry * page 139.
116 1.1 perry */
117 1.1 perry
118 1.3 christos for (j = t - 1; j > 0; j--) {
119 1.3 christos k = random() % (j + 1);
120 1.1 perry temp = shuffle[j];
121 1.1 perry shuffle[j] = shuffle[k];
122 1.1 perry shuffle[k] = temp;
123 1.1 perry }
124 1.1 perry
125 1.3 christos return shuffle;
126 1.1 perry }
127 1.1 perry
128 1.3 christos /*
129 1.3 christos * usage --
130 1.3 christos * Print a usage message and exit
131 1.3 christos */
132 1.3 christos static void
133 1.3 christos usage()
134 1.1 perry {
135 1.8 cgd
136 1.3 christos (void) fprintf(stderr,
137 1.7 is "Usage: %s [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
138 1.8 cgd getprogname());
139 1.3 christos exit(1);
140 1.1 perry }
141 1.1 perry
142 1.3 christos
143 1.3 christos /*
144 1.3 christos * get_lines --
145 1.3 christos * Return an array of lines read from input
146 1.3 christos */
147 1.3 christos static void
148 1.3 christos get_lines(fname, linesp, nlinesp)
149 1.3 christos const char *fname;
150 1.3 christos char ***linesp;
151 1.3 christos size_t *nlinesp;
152 1.1 perry {
153 1.3 christos FILE *fp;
154 1.3 christos char *line;
155 1.3 christos size_t size, nlines = 0, maxlines = 100;
156 1.3 christos char **lines = emalloc(sizeof(char *) * maxlines);
157 1.3 christos
158 1.3 christos if (strcmp(fname, "-") == 0)
159 1.3 christos fp = stdin;
160 1.3 christos else
161 1.3 christos if ((fp = fopen(fname, "r")) == NULL)
162 1.3 christos err(1, "Cannot open `%s'", fname);
163 1.3 christos
164 1.3 christos while ((line = fgetln(fp, &size)) != NULL) {
165 1.3 christos if (size > 0 && line[size - 1] == '\n')
166 1.3 christos size--;
167 1.3 christos lines[nlines] = emalloc(size + 1);
168 1.3 christos (void)memcpy(lines[nlines], line, size);
169 1.3 christos lines[nlines++][size] = '\0';
170 1.3 christos if (nlines >= maxlines) {
171 1.3 christos maxlines += 100;
172 1.3 christos lines = erealloc(lines, (sizeof(char *) * maxlines));
173 1.1 perry }
174 1.1 perry }
175 1.3 christos lines[nlines] = NULL;
176 1.1 perry
177 1.3 christos *linesp = lines;
178 1.3 christos *nlinesp = nlines;
179 1.3 christos if (strcmp(fname, "-") != 0)
180 1.3 christos (void)fclose(fp);
181 1.3 christos }
182 1.1 perry
183 1.3 christos /*
184 1.3 christos * get_number --
185 1.3 christos * Return a number or exit on error
186 1.3 christos */
187 1.3 christos static size_t
188 1.3 christos get_number(str, ch)
189 1.3 christos const char *str;
190 1.3 christos int ch;
191 1.3 christos {
192 1.3 christos char *estr;
193 1.3 christos long number = strtol(str, &estr, 0);
194 1.3 christos
195 1.3 christos if ((number == LONG_MIN || number == LONG_MAX) && errno == ERANGE)
196 1.3 christos err(1, "bad -%c argument `%s'", ch, str);
197 1.3 christos if (*estr)
198 1.3 christos errx(1, "non numeric -%c argument `%s'", ch, str);
199 1.3 christos if (number < 0)
200 1.3 christos errx(1, "negative -%c argument `%s'", ch, str);
201 1.3 christos return (size_t) number;
202 1.1 perry }
203 1.1 perry
204 1.1 perry int
205 1.3 christos main(argc, argv)
206 1.3 christos int argc;
207 1.3 christos char *argv[];
208 1.1 perry {
209 1.3 christos int i, nflag = 0, pflag = 0, ch;
210 1.3 christos char *fname = NULL;
211 1.6 fair size_t *shuffle = NULL;
212 1.1 perry struct timeval tv;
213 1.3 christos char **lines = NULL;
214 1.3 christos size_t nlines = 0, pick = 0;
215 1.2 perry
216 1.3 christos while ((ch = getopt(argc, argv, "f:n:p:")) != -1) {
217 1.1 perry switch(ch) {
218 1.3 christos case 'f':
219 1.3 christos fname = optarg;
220 1.1 perry break;
221 1.1 perry case 'n':
222 1.3 christos nlines = get_number(optarg, ch);
223 1.1 perry nflag = 1;
224 1.1 perry break;
225 1.1 perry case 'p':
226 1.3 christos pick = get_number(optarg, ch);
227 1.1 perry pflag = 1;
228 1.1 perry break;
229 1.1 perry case '?':
230 1.1 perry default:
231 1.1 perry usage();
232 1.1 perry }
233 1.1 perry }
234 1.1 perry argc -= optind;
235 1.1 perry argv += optind;
236 1.1 perry
237 1.3 christos if ((fname && nflag) || (nflag && (argc > 0)))
238 1.1 perry usage();
239 1.1 perry
240 1.3 christos if (fname != NULL)
241 1.3 christos get_lines(fname, &lines, &nlines);
242 1.3 christos else if (nflag == 0) {
243 1.3 christos lines = argv;
244 1.3 christos nlines = argc;
245 1.1 perry }
246 1.1 perry
247 1.1 perry gettimeofday(&tv, NULL);
248 1.1 perry srandom(getpid() ^ ~getuid() ^ tv.tv_sec ^ tv.tv_usec);
249 1.5 perry if (nlines > 0)
250 1.5 perry shuffle = get_shuffle(nlines);
251 1.1 perry
252 1.1 perry if (pflag) {
253 1.3 christos if (pick > nlines)
254 1.1 perry errx(1, "-p specified more components than exist.");
255 1.3 christos nlines = pick;
256 1.1 perry }
257 1.1 perry
258 1.3 christos for (i = 0; i < nlines; i++) {
259 1.1 perry if (nflag)
260 1.4 thorpej printf("%ld\n", (long)shuffle[i]);
261 1.1 perry else
262 1.3 christos printf("%s\n", lines[shuffle[i]]);
263 1.1 perry }
264 1.1 perry
265 1.3 christos return 0;
266 1.1 perry }
267