Home | History | Annotate | Line # | Download | only in shuffle
shuffle.c revision 1.20
      1  1.20     lukem /*	$NetBSD: shuffle.c,v 1.20 2009/04/13 07:31:36 lukem 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.20     lukem __RCSID("$NetBSD: shuffle.c,v 1.20 2009/04/13 07:31:36 lukem Exp $");
     37   1.1     perry #endif /* not lint */
     38   1.1     perry 
     39  1.11    simonb #include <sys/time.h>
     40  1.11    simonb 
     41   1.1     perry #include <err.h>
     42  1.11    simonb #include <errno.h>
     43  1.11    simonb #include <limits.h>
     44   1.1     perry #include <stdio.h>
     45   1.1     perry #include <stdlib.h>
     46   1.1     perry #include <string.h>
     47   1.1     perry #include <unistd.h>
     48  1.19  christos #include <util.h>
     49  1.10     perry 
     50  1.10     perry static size_t *get_shuffle(size_t);
     51  1.10     perry static void usage(void);
     52  1.10     perry static void get_lines(const char *, char ***, size_t *);
     53  1.10     perry static size_t get_number(const char *, int);
     54   1.2     perry 
     55  1.10     perry int main(int, char *[]);
     56   1.1     perry 
     57   1.1     perry /*
     58   1.3  christos  * get_shuffle --
     59   1.3  christos  *	Construct a random shuffle array of t elements
     60   1.3  christos  */
     61   1.3  christos static size_t *
     62  1.10     perry get_shuffle(size_t t)
     63   1.1     perry {
     64   1.3  christos 	size_t *shuffle;
     65   1.3  christos 	size_t i, j, k, temp;
     66   1.1     perry 
     67   1.3  christos 	shuffle = emalloc(t * sizeof(size_t));
     68   1.1     perry 
     69   1.1     perry 	for (i = 0; i < t; i++)
     70   1.1     perry 		shuffle[i] = i;
     71   1.1     perry 
     72   1.1     perry 	/*
     73   1.1     perry 	 * This algorithm taken from Knuth, Seminumerical Algorithms,
     74  1.18     perry 	 * 2nd Ed., page 139.
     75   1.1     perry 	 */
     76   1.1     perry 
     77   1.3  christos 	for (j = t - 1; j > 0; j--) {
     78  1.17    itojun 		k = arc4random() % (j + 1);
     79   1.1     perry 		temp = shuffle[j];
     80   1.1     perry 		shuffle[j] = shuffle[k];
     81   1.1     perry 		shuffle[k] = temp;
     82   1.1     perry 	}
     83   1.1     perry 
     84   1.3  christos 	return shuffle;
     85   1.1     perry }
     86   1.1     perry 
     87   1.3  christos /*
     88   1.3  christos  * usage --
     89   1.3  christos  *	Print a usage message and exit
     90   1.3  christos  */
     91   1.3  christos static void
     92  1.10     perry usage(void)
     93   1.1     perry {
     94   1.8       cgd 
     95   1.3  christos 	(void) fprintf(stderr,
     96  1.15      jmmv     "usage: %s [-0] [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
     97   1.8       cgd 		getprogname());
     98   1.3  christos 	exit(1);
     99   1.1     perry }
    100   1.1     perry 
    101   1.3  christos 
    102   1.3  christos /*
    103   1.3  christos  * get_lines --
    104   1.3  christos  *	Return an array of lines read from input
    105   1.3  christos  */
    106   1.3  christos static void
    107  1.10     perry get_lines(const char *fname, char ***linesp, size_t *nlinesp)
    108   1.1     perry {
    109   1.3  christos 	FILE *fp;
    110   1.3  christos 	char *line;
    111   1.9   mycroft 	size_t size, nlines = 0, maxlines = 128;
    112   1.3  christos 	char **lines = emalloc(sizeof(char *) * maxlines);
    113   1.3  christos 
    114   1.3  christos 	if (strcmp(fname, "-") == 0)
    115   1.3  christos 		fp = stdin;
    116   1.3  christos 	else
    117   1.3  christos 		if ((fp = fopen(fname, "r")) == NULL)
    118   1.3  christos 			err(1, "Cannot open `%s'", fname);
    119   1.3  christos 
    120   1.3  christos 	while ((line = fgetln(fp, &size)) != NULL) {
    121   1.3  christos 		if (size > 0 && line[size - 1] == '\n')
    122   1.3  christos 			size--;
    123   1.3  christos 		lines[nlines] = emalloc(size + 1);
    124   1.3  christos 		(void)memcpy(lines[nlines], line, size);
    125   1.3  christos 		lines[nlines++][size] = '\0';
    126   1.3  christos 		if (nlines >= maxlines) {
    127   1.9   mycroft 			maxlines *= 2;
    128   1.3  christos 			lines = erealloc(lines, (sizeof(char *) * maxlines));
    129   1.1     perry 		}
    130   1.1     perry 	}
    131   1.3  christos 	lines[nlines] = NULL;
    132   1.1     perry 
    133   1.3  christos 	*linesp = lines;
    134   1.3  christos 	*nlinesp = nlines;
    135   1.3  christos 	if (strcmp(fname, "-") != 0)
    136   1.3  christos 		(void)fclose(fp);
    137   1.3  christos }
    138   1.1     perry 
    139   1.3  christos /*
    140   1.3  christos  * get_number --
    141   1.3  christos  *	Return a number or exit on error
    142   1.3  christos  */
    143   1.3  christos static size_t
    144  1.10     perry get_number(const char *str, int ch)
    145   1.3  christos {
    146   1.3  christos 	char *estr;
    147  1.12     lukem 	long number;
    148   1.3  christos 
    149  1.12     lukem 	errno = 0;
    150  1.12     lukem 	number = strtol(str, &estr, 0);
    151   1.3  christos 	if ((number == LONG_MIN || number == LONG_MAX) && errno == ERANGE)
    152   1.3  christos 		err(1, "bad -%c argument `%s'", ch, str);
    153   1.3  christos 	if (*estr)
    154   1.3  christos 		errx(1, "non numeric -%c argument `%s'", ch, str);
    155   1.3  christos 	if (number < 0)
    156   1.3  christos 		errx(1, "negative -%c argument `%s'", ch, str);
    157   1.3  christos 	return (size_t) number;
    158   1.1     perry }
    159   1.1     perry 
    160   1.1     perry int
    161  1.10     perry main(int argc, char *argv[])
    162   1.1     perry {
    163  1.20     lukem 	int nflag = 0, pflag = 0, ch;
    164   1.3  christos 	char *fname = NULL;
    165   1.6      fair 	size_t *shuffle = NULL;
    166   1.3  christos 	char **lines = NULL;
    167  1.20     lukem 	size_t nlines = 0, pick = 0, i;
    168  1.13  christos 	char sep = '\n';
    169   1.2     perry 
    170  1.16      cube 	while ((ch = getopt(argc, argv, "0f:n:p:")) != -1) {
    171   1.1     perry 		switch(ch) {
    172  1.13  christos 		case '0':
    173  1.13  christos 			sep = '\0';
    174  1.13  christos 			break;
    175   1.3  christos 		case 'f':
    176   1.3  christos 			fname = optarg;
    177   1.1     perry 			break;
    178   1.1     perry 		case 'n':
    179   1.3  christos 			nlines = get_number(optarg, ch);
    180   1.1     perry 			nflag = 1;
    181   1.1     perry 			break;
    182   1.1     perry 		case 'p':
    183   1.3  christos 			pick = get_number(optarg, ch);
    184   1.1     perry 			pflag = 1;
    185   1.1     perry 			break;
    186   1.1     perry 		case '?':
    187   1.1     perry 		default:
    188   1.1     perry 			usage();
    189   1.1     perry 		}
    190   1.1     perry 	}
    191   1.1     perry 	argc -= optind;
    192   1.1     perry 	argv += optind;
    193   1.1     perry 
    194   1.3  christos 	if ((fname && nflag) || (nflag && (argc > 0)))
    195   1.1     perry 		usage();
    196   1.1     perry 
    197   1.3  christos 	if (fname != NULL)
    198   1.3  christos 		get_lines(fname, &lines, &nlines);
    199   1.3  christos 	else if (nflag == 0) {
    200   1.3  christos 		lines = argv;
    201   1.3  christos 		nlines = argc;
    202   1.1     perry 	}
    203   1.1     perry 
    204   1.5     perry 	if (nlines > 0)
    205   1.5     perry 		shuffle = get_shuffle(nlines);
    206   1.1     perry 
    207   1.1     perry 	if (pflag) {
    208   1.3  christos 		if (pick > nlines)
    209   1.1     perry 			errx(1, "-p specified more components than exist.");
    210   1.3  christos 		nlines = pick;
    211   1.1     perry 	}
    212   1.1     perry 
    213   1.3  christos 	for (i = 0; i < nlines; i++) {
    214   1.1     perry 		if (nflag)
    215  1.14  christos 			printf("%ld", (long)shuffle[i]);
    216   1.1     perry 		else
    217  1.14  christos 			printf("%s", lines[shuffle[i]]);
    218  1.14  christos 		putc(sep, stdout);
    219   1.1     perry 	}
    220   1.1     perry 
    221   1.3  christos 	return 0;
    222   1.1     perry }
    223