1 /* $NetBSD: shuffle.c,v 1.21 2011/09/16 15:39:29 joerg 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.21 2011/09/16 15:39:29 joerg Exp $");
50 static size_t *get_shuffle(size_t);
51 __dead
static void usage(void);
52 static void get_lines(const char *, char ***, size_t *);
53 static size_t get_number(const char *, int);
57 * Construct a random shuffle array of t elements
65 shuffle
= emalloc(t
* sizeof(size_t));
67 for (i
= 0; i
< t
; i
++)
71 * This algorithm taken from Knuth, Seminumerical Algorithms,
75 for (j
= t
- 1; j
> 0; j
--) {
76 k
= arc4random() % (j
+ 1);
78 shuffle
[j
] = shuffle
[k
];
87 * Print a usage message and exit
93 (void) fprintf(stderr
,
94 "usage: %s [-0] [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
102 * Return an array of lines read from input
105 get_lines(const char *fname
, char ***linesp
, size_t *nlinesp
)
109 size_t size
, nlines
= 0, maxlines
= 128;
110 char **lines
= emalloc(sizeof(char *) * maxlines
);
112 if (strcmp(fname
, "-") == 0)
115 if ((fp
= fopen(fname
, "r")) == NULL
)
116 err(1, "Cannot open `%s'", fname
);
118 while ((line
= fgetln(fp
, &size
)) != NULL
) {
119 if (size
> 0 && line
[size
- 1] == '\n')
121 lines
[nlines
] = emalloc(size
+ 1);
122 (void)memcpy(lines
[nlines
], line
, size
);
123 lines
[nlines
++][size
] = '\0';
124 if (nlines
>= maxlines
) {
126 lines
= erealloc(lines
, (sizeof(char *) * maxlines
));
129 lines
[nlines
] = NULL
;
133 if (strcmp(fname
, "-") != 0)
139 * Return a number or exit on error
142 get_number(const char *str
, int ch
)
148 number
= strtol(str
, &estr
, 0);
149 if ((number
== LONG_MIN
|| number
== LONG_MAX
) && errno
== ERANGE
)
150 err(1, "bad -%c argument `%s'", ch
, str
);
152 errx(1, "non numeric -%c argument `%s'", ch
, str
);
154 errx(1, "negative -%c argument `%s'", ch
, str
);
155 return (size_t) number
;
159 main(int argc
, char *argv
[])
161 int nflag
= 0, pflag
= 0, ch
;
163 size_t *shuffle
= NULL
;
165 size_t nlines
= 0, pick
= 0, i
;
168 while ((ch
= getopt(argc
, argv
, "0f:n:p:")) != -1) {
177 nlines
= get_number(optarg
, ch
);
181 pick
= get_number(optarg
, ch
);
192 if ((fname
&& nflag
) || (nflag
&& (argc
> 0)))
196 get_lines(fname
, &lines
, &nlines
);
197 else if (nflag
== 0) {
203 shuffle
= get_shuffle(nlines
);
207 errx(1, "-p specified more components than exist.");
211 for (i
= 0; i
< nlines
; i
++) {
213 printf("%ld", (long)shuffle
[i
]);
215 printf("%s", lines
[shuffle
[i
]]);