Home | History | Annotate | Line # | Download | only in shuffle
shuffle.c revision 1.10
      1  1.10     perry /*	$NetBSD: shuffle.c,v 1.10 2001/05/29 23:22:42 perry Exp $	*/
      2   1.1     perry 
      3   1.1     perry /*
      4   1.1     perry  * Copyright (c) 1998
      5   1.1     perry  * 	Perry E. Metzger.  All rights reserved.
      6   1.1     perry  *
      7   1.1     perry  * Redistribution and use in source and binary forms, with or without
      8   1.1     perry  * modification, are permitted provided that the following conditions
      9   1.1     perry  * are met:
     10   1.1     perry  * 1. Redistributions of source code must retain the above copyright
     11   1.1     perry  *    notice, this list of conditions and the following disclaimer.
     12   1.1     perry  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     perry  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     perry  *    documentation and/or other materials provided with the distribution.
     15   1.1     perry  * 3. All advertising materials mentioning features or use of this software
     16   1.1     perry  *    must display the following acknowledgment:
     17   1.1     perry  *	This product includes software developed for the NetBSD Project
     18   1.1     perry  *	by Perry E. Metzger.
     19   1.1     perry  * 4. The name of the author may not be used to endorse or promote products
     20   1.1     perry  *    derived from this software without specific prior written permission.
     21   1.1     perry  *
     22   1.1     perry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1     perry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1     perry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1     perry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1     perry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1     perry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1     perry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1     perry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1     perry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1     perry  */
     33   1.1     perry 
     34   1.1     perry #include <sys/cdefs.h>
     35   1.1     perry #ifndef lint
     36  1.10     perry __RCSID("$NetBSD: shuffle.c,v 1.10 2001/05/29 23:22:42 perry Exp $");
     37   1.1     perry #endif /* not lint */
     38   1.1     perry 
     39   1.1     perry #include <err.h>
     40   1.1     perry #include <stdio.h>
     41   1.1     perry #include <stdlib.h>
     42   1.1     perry #include <string.h>
     43   1.1     perry #include <unistd.h>
     44   1.3  christos #include <errno.h>
     45   1.1     perry #include <sys/time.h>
     46   1.1     perry 
     47  1.10     perry static void enomem(void);
     48  1.10     perry static void *emalloc(size_t);
     49  1.10     perry static void *erealloc(void *, size_t);
     50  1.10     perry 
     51  1.10     perry static size_t *get_shuffle(size_t);
     52  1.10     perry static void usage(void);
     53  1.10     perry static void get_lines(const char *, char ***, size_t *);
     54  1.10     perry static size_t get_number(const char *, int);
     55   1.2     perry 
     56  1.10     perry int main(int, char *[]);
     57   1.1     perry 
     58   1.1     perry /*
     59   1.1     perry  * enomem --
     60   1.1     perry  *	die when out of memory.
     61   1.1     perry  */
     62   1.3  christos static void
     63  1.10     perry enomem(void)
     64   1.1     perry {
     65   1.1     perry 	errx(2, "Cannot allocate memory.");
     66   1.1     perry }
     67   1.1     perry 
     68   1.1     perry /*
     69   1.1     perry  * emalloc --
     70   1.1     perry  *	malloc, but die on error.
     71   1.1     perry  */
     72   1.3  christos static void *
     73  1.10     perry emalloc(size_t len)
     74   1.1     perry {
     75   1.1     perry 	void *p;
     76   1.1     perry 
     77   1.1     perry 	if ((p = malloc(len)) == NULL)
     78   1.1     perry 		enomem();
     79   1.3  christos 	return p;
     80   1.1     perry }
     81   1.1     perry 
     82   1.1     perry /*
     83   1.1     perry  * erealloc --
     84   1.1     perry  *	realloc, but die on error.
     85   1.1     perry  */
     86   1.1     perry void *
     87  1.10     perry erealloc(void *ptr, size_t size)
     88   1.1     perry {
     89   1.1     perry 	if ((ptr = realloc(ptr, size)) == NULL)
     90   1.1     perry 		enomem();
     91   1.3  christos 	return ptr;
     92   1.1     perry }
     93   1.1     perry 
     94   1.3  christos /*
     95   1.3  christos  * get_shuffle --
     96   1.3  christos  *	Construct a random shuffle array of t elements
     97   1.3  christos  */
     98   1.3  christos static size_t *
     99  1.10     perry get_shuffle(size_t t)
    100   1.1     perry {
    101   1.3  christos 	size_t *shuffle;
    102   1.3  christos 	size_t i, j, k, temp;
    103   1.1     perry 
    104   1.3  christos 	shuffle = emalloc(t * sizeof(size_t));
    105   1.1     perry 
    106   1.1     perry 	for (i = 0; i < t; i++)
    107   1.1     perry 		shuffle[i] = i;
    108   1.1     perry 
    109   1.1     perry 	/*
    110   1.1     perry 	 * This algorithm taken from Knuth, Seminumerical Algorithms,
    111   1.1     perry 	 * page 139.
    112   1.1     perry 	 */
    113   1.1     perry 
    114   1.3  christos 	for (j = t - 1; j > 0; j--) {
    115   1.3  christos 		k = random() % (j + 1);
    116   1.1     perry 		temp = shuffle[j];
    117   1.1     perry 		shuffle[j] = shuffle[k];
    118   1.1     perry 		shuffle[k] = temp;
    119   1.1     perry 	}
    120   1.1     perry 
    121   1.3  christos 	return shuffle;
    122   1.1     perry }
    123   1.1     perry 
    124   1.3  christos /*
    125   1.3  christos  * usage --
    126   1.3  christos  *	Print a usage message and exit
    127   1.3  christos  */
    128   1.3  christos static void
    129  1.10     perry usage(void)
    130   1.1     perry {
    131   1.8       cgd 
    132   1.3  christos 	(void) fprintf(stderr,
    133   1.7        is     "Usage: %s [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
    134   1.8       cgd 		getprogname());
    135   1.3  christos 	exit(1);
    136   1.1     perry }
    137   1.1     perry 
    138   1.3  christos 
    139   1.3  christos /*
    140   1.3  christos  * get_lines --
    141   1.3  christos  *	Return an array of lines read from input
    142   1.3  christos  */
    143   1.3  christos static void
    144  1.10     perry get_lines(const char *fname, char ***linesp, size_t *nlinesp)
    145   1.1     perry {
    146   1.3  christos 	FILE *fp;
    147   1.3  christos 	char *line;
    148   1.9   mycroft 	size_t size, nlines = 0, maxlines = 128;
    149   1.3  christos 	char **lines = emalloc(sizeof(char *) * maxlines);
    150   1.3  christos 
    151   1.3  christos 	if (strcmp(fname, "-") == 0)
    152   1.3  christos 		fp = stdin;
    153   1.3  christos 	else
    154   1.3  christos 		if ((fp = fopen(fname, "r")) == NULL)
    155   1.3  christos 			err(1, "Cannot open `%s'", fname);
    156   1.3  christos 
    157   1.3  christos 	while ((line = fgetln(fp, &size)) != NULL) {
    158   1.3  christos 		if (size > 0 && line[size - 1] == '\n')
    159   1.3  christos 			size--;
    160   1.3  christos 		lines[nlines] = emalloc(size + 1);
    161   1.3  christos 		(void)memcpy(lines[nlines], line, size);
    162   1.3  christos 		lines[nlines++][size] = '\0';
    163   1.3  christos 		if (nlines >= maxlines) {
    164   1.9   mycroft 			maxlines *= 2;
    165   1.3  christos 			lines = erealloc(lines, (sizeof(char *) * maxlines));
    166   1.1     perry 		}
    167   1.1     perry 	}
    168   1.3  christos 	lines[nlines] = NULL;
    169   1.1     perry 
    170   1.3  christos 	*linesp = lines;
    171   1.3  christos 	*nlinesp = nlines;
    172   1.3  christos 	if (strcmp(fname, "-") != 0)
    173   1.3  christos 		(void)fclose(fp);
    174   1.3  christos }
    175   1.1     perry 
    176   1.3  christos /*
    177   1.3  christos  * get_number --
    178   1.3  christos  *	Return a number or exit on error
    179   1.3  christos  */
    180   1.3  christos static size_t
    181  1.10     perry get_number(const char *str, int ch)
    182   1.3  christos {
    183   1.3  christos 	char *estr;
    184   1.3  christos 	long number = strtol(str, &estr, 0);
    185   1.3  christos 
    186   1.3  christos 	if ((number == LONG_MIN || number == LONG_MAX) && errno == ERANGE)
    187   1.3  christos 		err(1, "bad -%c argument `%s'", ch, str);
    188   1.3  christos 	if (*estr)
    189   1.3  christos 		errx(1, "non numeric -%c argument `%s'", ch, str);
    190   1.3  christos 	if (number < 0)
    191   1.3  christos 		errx(1, "negative -%c argument `%s'", ch, str);
    192   1.3  christos 	return (size_t) number;
    193   1.1     perry }
    194   1.1     perry 
    195   1.1     perry int
    196  1.10     perry main(int argc, char *argv[])
    197   1.1     perry {
    198   1.3  christos 	int i, nflag = 0, pflag = 0, ch;
    199   1.3  christos 	char *fname = NULL;
    200   1.6      fair 	size_t *shuffle = NULL;
    201   1.1     perry 	struct timeval tv;
    202   1.3  christos 	char **lines = NULL;
    203   1.3  christos 	size_t nlines = 0, pick = 0;
    204   1.2     perry 
    205   1.3  christos 	while ((ch = getopt(argc, argv, "f:n:p:")) != -1) {
    206   1.1     perry 		switch(ch) {
    207   1.3  christos 		case 'f':
    208   1.3  christos 			fname = optarg;
    209   1.1     perry 			break;
    210   1.1     perry 		case 'n':
    211   1.3  christos 			nlines = get_number(optarg, ch);
    212   1.1     perry 			nflag = 1;
    213   1.1     perry 			break;
    214   1.1     perry 		case 'p':
    215   1.3  christos 			pick = get_number(optarg, ch);
    216   1.1     perry 			pflag = 1;
    217   1.1     perry 			break;
    218   1.1     perry 		case '?':
    219   1.1     perry 		default:
    220   1.1     perry 			usage();
    221   1.1     perry 		}
    222   1.1     perry 	}
    223   1.1     perry 	argc -= optind;
    224   1.1     perry 	argv += optind;
    225   1.1     perry 
    226   1.3  christos 	if ((fname && nflag) || (nflag && (argc > 0)))
    227   1.1     perry 		usage();
    228   1.1     perry 
    229   1.3  christos 	if (fname != NULL)
    230   1.3  christos 		get_lines(fname, &lines, &nlines);
    231   1.3  christos 	else if (nflag == 0) {
    232   1.3  christos 		lines = argv;
    233   1.3  christos 		nlines = argc;
    234   1.1     perry 	}
    235   1.1     perry 
    236   1.1     perry 	gettimeofday(&tv, NULL);
    237   1.1     perry 	srandom(getpid() ^ ~getuid() ^ tv.tv_sec ^ tv.tv_usec);
    238   1.5     perry 	if (nlines > 0)
    239   1.5     perry 		shuffle = get_shuffle(nlines);
    240   1.1     perry 
    241   1.1     perry 	if (pflag) {
    242   1.3  christos 		if (pick > nlines)
    243   1.1     perry 			errx(1, "-p specified more components than exist.");
    244   1.3  christos 		nlines = pick;
    245   1.1     perry 	}
    246   1.1     perry 
    247   1.3  christos 	for (i = 0; i < nlines; i++) {
    248   1.1     perry 		if (nflag)
    249   1.4   thorpej 			printf("%ld\n", (long)shuffle[i]);
    250   1.1     perry 		else
    251   1.3  christos 			printf("%s\n", lines[shuffle[i]]);
    252   1.1     perry 	}
    253   1.1     perry 
    254   1.3  christos 	return 0;
    255   1.1     perry }
    256