2001-09-01 06:17:29 +04:00
|
|
|
/* $NetBSD: shuffle.c,v 1.11 2001/09/01 02:17:29 simonb 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
|
2001-09-01 06:17:29 +04:00
|
|
|
__RCSID("$NetBSD: shuffle.c,v 1.11 2001/09/01 02:17:29 simonb Exp $");
|
1998-09-24 01:05:59 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2001-09-01 06:17:29 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
1998-09-24 01:05:59 +04:00
|
|
|
#include <err.h>
|
2001-09-01 06:17:29 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
1998-09-24 01:05:59 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-05-30 03:22:42 +04:00
|
|
|
static void enomem(void);
|
|
|
|
static void *emalloc(size_t);
|
|
|
|
static void *erealloc(void *, size_t);
|
1998-09-24 01:45:44 +04:00
|
|
|
|
2001-05-30 03:22:42 +04:00
|
|
|
static size_t *get_shuffle(size_t);
|
|
|
|
static void usage(void);
|
|
|
|
static void get_lines(const char *, char ***, size_t *);
|
|
|
|
static size_t get_number(const char *, int);
|
1998-09-24 01:45:44 +04:00
|
|
|
|
2001-05-30 03:22:42 +04:00
|
|
|
int main(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
|
2001-05-30 03:22:42 +04:00
|
|
|
enomem(void)
|
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 *
|
2001-05-30 03:22:42 +04:00
|
|
|
emalloc(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 *
|
2001-05-30 03:22:42 +04:00
|
|
|
erealloc(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 *
|
2001-05-30 03:22:42 +04:00
|
|
|
get_shuffle(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
|
2001-05-30 03:22:42 +04:00
|
|
|
usage(void)
|
1998-09-24 01:05:59 +04:00
|
|
|
{
|
2001-02-20 02:03:42 +03:00
|
|
|
|
1998-09-24 16:28:39 +04:00
|
|
|
(void) fprintf(stderr,
|
1999-02-03 19:22:16 +03:00
|
|
|
"Usage: %s [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
|
2001-02-20 02:03:42 +03:00
|
|
|
getprogname());
|
1998-09-24 16:28:39 +04:00
|
|
|
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
|
2001-05-30 03:22:42 +04:00
|
|
|
get_lines(const char *fname, char ***linesp, size_t *nlinesp)
|
1998-09-24 16:28:39 +04:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char *line;
|
2001-03-16 11:02:32 +03:00
|
|
|
size_t size, nlines = 0, maxlines = 128;
|
1998-09-24 16:28:39 +04:00
|
|
|
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) {
|
2001-03-16 11:02:32 +03:00
|
|
|
maxlines *= 2;
|
1998-09-24 16:28:39 +04:00
|
|
|
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
|
2001-05-30 03:22:42 +04:00
|
|
|
get_number(const char *str, int ch)
|
1998-09-24 16:28:39 +04:00
|
|
|
{
|
|
|
|
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
|
2001-05-30 03:22:42 +04:00
|
|
|
main(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
|
|
|
}
|