1998-12-07 07:10:13 +03:00
|
|
|
/* $NetBSD: shuffle.c,v 1.6 1998/12/07 04:10:13 fair Exp $ */
|
1998-09-24 01:05:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998
|
|
|
|
* Perry E. Metzger. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgment:
|
|
|
|
* This product includes software developed for the NetBSD Project
|
|
|
|
* by Perry E. Metzger.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
1998-12-07 07:10:13 +03:00
|
|
|
__RCSID("$NetBSD: shuffle.c,v 1.6 1998/12/07 04:10:13 fair Exp $");
|
1998-09-24 01:05:59 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1998-09-24 16:28:39 +04:00
|
|
|
#include <errno.h>
|
1998-09-24 01:05:59 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
static void enomem __P((void));
|
|
|
|
static void *emalloc __P((size_t));
|
|
|
|
static void *erealloc __P((void *, size_t));
|
1998-09-24 01:45:44 +04:00
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
static size_t *get_shuffle __P((size_t));
|
|
|
|
static void usage __P((void));
|
|
|
|
static void get_lines __P((const char *, char ***, size_t *));
|
|
|
|
static size_t get_number __P((const char *, int));
|
1998-09-24 01:45:44 +04:00
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
int main __P((int, char *[]));
|
1998-09-24 01:05:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* enomem --
|
|
|
|
* die when out of memory.
|
|
|
|
*/
|
1998-09-24 16:28:39 +04:00
|
|
|
static void
|
|
|
|
enomem()
|
1998-09-24 01:05:59 +04:00
|
|
|
{
|
|
|
|
errx(2, "Cannot allocate memory.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* emalloc --
|
|
|
|
* malloc, but die on error.
|
|
|
|
*/
|
1998-09-24 16:28:39 +04:00
|
|
|
static void *
|
|
|
|
emalloc(len)
|
|
|
|
size_t len;
|
1998-09-24 01:05:59 +04:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
if ((p = malloc(len)) == NULL)
|
|
|
|
enomem();
|
1998-09-24 16:28:39 +04:00
|
|
|
return p;
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* erealloc --
|
|
|
|
* realloc, but die on error.
|
|
|
|
*/
|
|
|
|
void *
|
1998-09-24 16:28:39 +04:00
|
|
|
erealloc(ptr, size)
|
|
|
|
void *ptr;
|
|
|
|
size_t size;
|
1998-09-24 01:05:59 +04:00
|
|
|
{
|
|
|
|
if ((ptr = realloc(ptr, size)) == NULL)
|
|
|
|
enomem();
|
1998-09-24 16:28:39 +04:00
|
|
|
return ptr;
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
/*
|
|
|
|
* get_shuffle --
|
|
|
|
* Construct a random shuffle array of t elements
|
|
|
|
*/
|
|
|
|
static size_t *
|
|
|
|
get_shuffle(t)
|
|
|
|
size_t t;
|
1998-09-24 01:05:59 +04:00
|
|
|
{
|
1998-09-24 16:28:39 +04:00
|
|
|
size_t *shuffle;
|
|
|
|
size_t i, j, k, temp;
|
1998-09-24 01:05:59 +04:00
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
shuffle = emalloc(t * sizeof(size_t));
|
1998-09-24 01:05:59 +04:00
|
|
|
|
|
|
|
for (i = 0; i < t; i++)
|
|
|
|
shuffle[i] = i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This algorithm taken from Knuth, Seminumerical Algorithms,
|
|
|
|
* page 139.
|
|
|
|
*/
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
for (j = t - 1; j > 0; j--) {
|
|
|
|
k = random() % (j + 1);
|
1998-09-24 01:05:59 +04:00
|
|
|
temp = shuffle[j];
|
|
|
|
shuffle[j] = shuffle[k];
|
|
|
|
shuffle[k] = temp;
|
|
|
|
}
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
return shuffle;
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
/*
|
|
|
|
* usage --
|
|
|
|
* Print a usage message and exit
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
usage()
|
1998-09-24 01:05:59 +04:00
|
|
|
{
|
1998-09-24 16:28:39 +04:00
|
|
|
extern char *__progname;
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"Usage: %s [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]",
|
|
|
|
__progname);
|
|
|
|
exit(1);
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
/*
|
|
|
|
* get_lines --
|
|
|
|
* Return an array of lines read from input
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
get_lines(fname, linesp, nlinesp)
|
|
|
|
const char *fname;
|
|
|
|
char ***linesp;
|
|
|
|
size_t *nlinesp;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char *line;
|
|
|
|
size_t size, nlines = 0, maxlines = 100;
|
|
|
|
char **lines = emalloc(sizeof(char *) * maxlines);
|
|
|
|
|
|
|
|
if (strcmp(fname, "-") == 0)
|
|
|
|
fp = stdin;
|
|
|
|
else
|
|
|
|
if ((fp = fopen(fname, "r")) == NULL)
|
|
|
|
err(1, "Cannot open `%s'", fname);
|
|
|
|
|
|
|
|
while ((line = fgetln(fp, &size)) != NULL) {
|
|
|
|
if (size > 0 && line[size - 1] == '\n')
|
|
|
|
size--;
|
|
|
|
lines[nlines] = emalloc(size + 1);
|
|
|
|
(void)memcpy(lines[nlines], line, size);
|
|
|
|
lines[nlines++][size] = '\0';
|
|
|
|
if (nlines >= maxlines) {
|
|
|
|
maxlines += 100;
|
|
|
|
lines = erealloc(lines, (sizeof(char *) * maxlines));
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
}
|
1998-09-24 16:28:39 +04:00
|
|
|
lines[nlines] = NULL;
|
1998-09-24 01:05:59 +04:00
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
*linesp = lines;
|
|
|
|
*nlinesp = nlines;
|
|
|
|
if (strcmp(fname, "-") != 0)
|
|
|
|
(void)fclose(fp);
|
|
|
|
}
|
1998-09-24 01:05:59 +04:00
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
/*
|
|
|
|
* get_number --
|
|
|
|
* Return a number or exit on error
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
get_number(str, ch)
|
|
|
|
const char *str;
|
|
|
|
int ch;
|
|
|
|
{
|
|
|
|
char *estr;
|
|
|
|
long number = strtol(str, &estr, 0);
|
|
|
|
|
|
|
|
if ((number == LONG_MIN || number == LONG_MAX) && errno == ERANGE)
|
|
|
|
err(1, "bad -%c argument `%s'", ch, str);
|
|
|
|
if (*estr)
|
|
|
|
errx(1, "non numeric -%c argument `%s'", ch, str);
|
|
|
|
if (number < 0)
|
|
|
|
errx(1, "negative -%c argument `%s'", ch, str);
|
|
|
|
return (size_t) number;
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-09-24 16:28:39 +04:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1998-09-24 01:05:59 +04:00
|
|
|
{
|
1998-09-24 16:28:39 +04:00
|
|
|
int i, nflag = 0, pflag = 0, ch;
|
|
|
|
char *fname = NULL;
|
1998-12-07 07:10:13 +03:00
|
|
|
size_t *shuffle = NULL;
|
1998-09-24 01:05:59 +04:00
|
|
|
struct timeval tv;
|
1998-09-24 16:28:39 +04:00
|
|
|
char **lines = NULL;
|
|
|
|
size_t nlines = 0, pick = 0;
|
1998-09-24 01:45:44 +04:00
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
while ((ch = getopt(argc, argv, "f:n:p:")) != -1) {
|
1998-09-24 01:05:59 +04:00
|
|
|
switch(ch) {
|
1998-09-24 16:28:39 +04:00
|
|
|
case 'f':
|
|
|
|
fname = optarg;
|
1998-09-24 01:05:59 +04:00
|
|
|
break;
|
|
|
|
case 'n':
|
1998-09-24 16:28:39 +04:00
|
|
|
nlines = get_number(optarg, ch);
|
1998-09-24 01:05:59 +04:00
|
|
|
nflag = 1;
|
|
|
|
break;
|
|
|
|
case 'p':
|
1998-09-24 16:28:39 +04:00
|
|
|
pick = get_number(optarg, ch);
|
1998-09-24 01:05:59 +04:00
|
|
|
pflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
if ((fname && nflag) || (nflag && (argc > 0)))
|
1998-09-24 01:05:59 +04:00
|
|
|
usage();
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
if (fname != NULL)
|
|
|
|
get_lines(fname, &lines, &nlines);
|
|
|
|
else if (nflag == 0) {
|
|
|
|
lines = argv;
|
|
|
|
nlines = argc;
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
srandom(getpid() ^ ~getuid() ^ tv.tv_sec ^ tv.tv_usec);
|
1998-12-04 20:47:06 +03:00
|
|
|
if (nlines > 0)
|
|
|
|
shuffle = get_shuffle(nlines);
|
1998-09-24 01:05:59 +04:00
|
|
|
|
|
|
|
if (pflag) {
|
1998-09-24 16:28:39 +04:00
|
|
|
if (pick > nlines)
|
1998-09-24 01:05:59 +04:00
|
|
|
errx(1, "-p specified more components than exist.");
|
1998-09-24 16:28:39 +04:00
|
|
|
nlines = pick;
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
for (i = 0; i < nlines; i++) {
|
1998-09-24 01:05:59 +04:00
|
|
|
if (nflag)
|
1998-09-30 22:37:05 +04:00
|
|
|
printf("%ld\n", (long)shuffle[i]);
|
1998-09-24 01:05:59 +04:00
|
|
|
else
|
1998-09-24 16:28:39 +04:00
|
|
|
printf("%s\n", lines[shuffle[i]]);
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
return 0;
|
1998-09-24 01:05:59 +04:00
|
|
|
}
|