Merge from vendor branch PKGSRC:
[netbsd-mini2440.git] / usr.bin / shuffle / shuffle.c
blob9e0c988aad1ea60e992db7893dc16b985d7f948e
1 /* $NetBSD: shuffle.c,v 1.19 2006/08/26 18:17:43 christos Exp $ */
3 /*
4 * Copyright (c) 1998
5 * Perry E. Metzger. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgment:
17 * This product includes software developed for the NetBSD Project
18 * by Perry E. Metzger.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: shuffle.c,v 1.19 2006/08/26 18:17:43 christos Exp $");
37 #endif /* not lint */
39 #include <sys/time.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <util.h>
50 static size_t *get_shuffle(size_t);
51 static void usage(void);
52 static void get_lines(const char *, char ***, size_t *);
53 static size_t get_number(const char *, int);
55 int main(int, char *[]);
58 * get_shuffle --
59 * Construct a random shuffle array of t elements
61 static size_t *
62 get_shuffle(size_t t)
64 size_t *shuffle;
65 size_t i, j, k, temp;
67 shuffle = emalloc(t * sizeof(size_t));
69 for (i = 0; i < t; i++)
70 shuffle[i] = i;
73 * This algorithm taken from Knuth, Seminumerical Algorithms,
74 * 2nd Ed., page 139.
77 for (j = t - 1; j > 0; j--) {
78 k = arc4random() % (j + 1);
79 temp = shuffle[j];
80 shuffle[j] = shuffle[k];
81 shuffle[k] = temp;
84 return shuffle;
88 * usage --
89 * Print a usage message and exit
91 static void
92 usage(void)
95 (void) fprintf(stderr,
96 "usage: %s [-0] [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
97 getprogname());
98 exit(1);
103 * get_lines --
104 * Return an array of lines read from input
106 static void
107 get_lines(const char *fname, char ***linesp, size_t *nlinesp)
109 FILE *fp;
110 char *line;
111 size_t size, nlines = 0, maxlines = 128;
112 char **lines = emalloc(sizeof(char *) * maxlines);
114 if (strcmp(fname, "-") == 0)
115 fp = stdin;
116 else
117 if ((fp = fopen(fname, "r")) == NULL)
118 err(1, "Cannot open `%s'", fname);
120 while ((line = fgetln(fp, &size)) != NULL) {
121 if (size > 0 && line[size - 1] == '\n')
122 size--;
123 lines[nlines] = emalloc(size + 1);
124 (void)memcpy(lines[nlines], line, size);
125 lines[nlines++][size] = '\0';
126 if (nlines >= maxlines) {
127 maxlines *= 2;
128 lines = erealloc(lines, (sizeof(char *) * maxlines));
131 lines[nlines] = NULL;
133 *linesp = lines;
134 *nlinesp = nlines;
135 if (strcmp(fname, "-") != 0)
136 (void)fclose(fp);
140 * get_number --
141 * Return a number or exit on error
143 static size_t
144 get_number(const char *str, int ch)
146 char *estr;
147 long number;
149 errno = 0;
150 number = strtol(str, &estr, 0);
151 if ((number == LONG_MIN || number == LONG_MAX) && errno == ERANGE)
152 err(1, "bad -%c argument `%s'", ch, str);
153 if (*estr)
154 errx(1, "non numeric -%c argument `%s'", ch, str);
155 if (number < 0)
156 errx(1, "negative -%c argument `%s'", ch, str);
157 return (size_t) number;
161 main(int argc, char *argv[])
163 int nflag = 0, pflag = 0, ch;
164 char *fname = NULL;
165 size_t *shuffle = NULL;
166 char **lines = NULL;
167 size_t nlines = 0, pick = 0, i;
168 char sep = '\n';
170 while ((ch = getopt(argc, argv, "0f:n:p:")) != -1) {
171 switch(ch) {
172 case '0':
173 sep = '\0';
174 break;
175 case 'f':
176 fname = optarg;
177 break;
178 case 'n':
179 nlines = get_number(optarg, ch);
180 nflag = 1;
181 break;
182 case 'p':
183 pick = get_number(optarg, ch);
184 pflag = 1;
185 break;
186 case '?':
187 default:
188 usage();
191 argc -= optind;
192 argv += optind;
194 if ((fname && nflag) || (nflag && (argc > 0)))
195 usage();
197 if (fname != NULL)
198 get_lines(fname, &lines, &nlines);
199 else if (nflag == 0) {
200 lines = argv;
201 nlines = argc;
204 if (nlines > 0)
205 shuffle = get_shuffle(nlines);
207 if (pflag) {
208 if (pick > nlines)
209 errx(1, "-p specified more components than exist.");
210 nlines = pick;
213 for (i = 0; i < nlines; i++) {
214 if (nflag)
215 printf("%ld", (long)shuffle[i]);
216 else
217 printf("%s", lines[shuffle[i]]);
218 putc(sep, stdout);
221 return 0;