1 /* $NetBSD: shuffle.c,v 1.19 2006/08/26 18:17:43 christos Exp $ */
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
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>
36 __RCSID("$NetBSD: shuffle.c,v 1.19 2006/08/26 18:17:43 christos Exp $");
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 *[]);
59 * Construct a random shuffle array of t elements
67 shuffle
= emalloc(t
* sizeof(size_t));
69 for (i
= 0; i
< t
; i
++)
73 * This algorithm taken from Knuth, Seminumerical Algorithms,
77 for (j
= t
- 1; j
> 0; j
--) {
78 k
= arc4random() % (j
+ 1);
80 shuffle
[j
] = shuffle
[k
];
89 * Print a usage message and exit
95 (void) fprintf(stderr
,
96 "usage: %s [-0] [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
104 * Return an array of lines read from input
107 get_lines(const char *fname
, char ***linesp
, size_t *nlinesp
)
111 size_t size
, nlines
= 0, maxlines
= 128;
112 char **lines
= emalloc(sizeof(char *) * maxlines
);
114 if (strcmp(fname
, "-") == 0)
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')
123 lines
[nlines
] = emalloc(size
+ 1);
124 (void)memcpy(lines
[nlines
], line
, size
);
125 lines
[nlines
++][size
] = '\0';
126 if (nlines
>= maxlines
) {
128 lines
= erealloc(lines
, (sizeof(char *) * maxlines
));
131 lines
[nlines
] = NULL
;
135 if (strcmp(fname
, "-") != 0)
141 * Return a number or exit on error
144 get_number(const char *str
, int ch
)
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
);
154 errx(1, "non numeric -%c argument `%s'", ch
, str
);
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
;
165 size_t *shuffle
= NULL
;
167 size_t nlines
= 0, pick
= 0, i
;
170 while ((ch
= getopt(argc
, argv
, "0f:n:p:")) != -1) {
179 nlines
= get_number(optarg
, ch
);
183 pick
= get_number(optarg
, ch
);
194 if ((fname
&& nflag
) || (nflag
&& (argc
> 0)))
198 get_lines(fname
, &lines
, &nlines
);
199 else if (nflag
== 0) {
205 shuffle
= get_shuffle(nlines
);
209 errx(1, "-p specified more components than exist.");
213 for (i
= 0; i
< nlines
; i
++) {
215 printf("%ld", (long)shuffle
[i
]);
217 printf("%s", lines
[shuffle
[i
]]);