Home | History | Annotate | Line # | Download | only in shuffle
shuffle.c revision 1.15.2.1
      1  1.15.2.1      tron /*	$NetBSD: shuffle.c,v 1.15.2.1 2004/05/20 09:45:50 tron 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.15.2.1      tron __RCSID("$NetBSD: shuffle.c,v 1.15.2.1 2004/05/20 09:45:50 tron 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.1     perry 
     49      1.10     perry static void enomem(void);
     50      1.10     perry static void *emalloc(size_t);
     51      1.10     perry static void *erealloc(void *, size_t);
     52      1.10     perry 
     53      1.10     perry static size_t *get_shuffle(size_t);
     54      1.10     perry static void usage(void);
     55      1.10     perry static void get_lines(const char *, char ***, size_t *);
     56      1.10     perry static size_t get_number(const char *, int);
     57       1.2     perry 
     58      1.10     perry int main(int, char *[]);
     59       1.1     perry 
     60       1.1     perry /*
     61       1.1     perry  * enomem --
     62       1.1     perry  *	die when out of memory.
     63       1.1     perry  */
     64       1.3  christos static void
     65      1.10     perry enomem(void)
     66       1.1     perry {
     67       1.1     perry 	errx(2, "Cannot allocate memory.");
     68       1.1     perry }
     69       1.1     perry 
     70       1.1     perry /*
     71       1.1     perry  * emalloc --
     72       1.1     perry  *	malloc, but die on error.
     73       1.1     perry  */
     74       1.3  christos static void *
     75      1.10     perry emalloc(size_t len)
     76       1.1     perry {
     77       1.1     perry 	void *p;
     78       1.1     perry 
     79       1.1     perry 	if ((p = malloc(len)) == NULL)
     80       1.1     perry 		enomem();
     81       1.3  christos 	return p;
     82       1.1     perry }
     83       1.1     perry 
     84       1.1     perry /*
     85       1.1     perry  * erealloc --
     86       1.1     perry  *	realloc, but die on error.
     87       1.1     perry  */
     88       1.1     perry void *
     89      1.10     perry erealloc(void *ptr, size_t size)
     90       1.1     perry {
     91       1.1     perry 	if ((ptr = realloc(ptr, size)) == NULL)
     92       1.1     perry 		enomem();
     93       1.3  christos 	return ptr;
     94       1.1     perry }
     95       1.1     perry 
     96       1.3  christos /*
     97       1.3  christos  * get_shuffle --
     98       1.3  christos  *	Construct a random shuffle array of t elements
     99       1.3  christos  */
    100       1.3  christos static size_t *
    101      1.10     perry get_shuffle(size_t t)
    102       1.1     perry {
    103       1.3  christos 	size_t *shuffle;
    104       1.3  christos 	size_t i, j, k, temp;
    105       1.1     perry 
    106       1.3  christos 	shuffle = emalloc(t * sizeof(size_t));
    107       1.1     perry 
    108       1.1     perry 	for (i = 0; i < t; i++)
    109       1.1     perry 		shuffle[i] = i;
    110       1.1     perry 
    111       1.1     perry 	/*
    112       1.1     perry 	 * This algorithm taken from Knuth, Seminumerical Algorithms,
    113       1.1     perry 	 * page 139.
    114       1.1     perry 	 */
    115       1.1     perry 
    116       1.3  christos 	for (j = t - 1; j > 0; j--) {
    117       1.3  christos 		k = random() % (j + 1);
    118       1.1     perry 		temp = shuffle[j];
    119       1.1     perry 		shuffle[j] = shuffle[k];
    120       1.1     perry 		shuffle[k] = temp;
    121       1.1     perry 	}
    122       1.1     perry 
    123       1.3  christos 	return shuffle;
    124       1.1     perry }
    125       1.1     perry 
    126       1.3  christos /*
    127       1.3  christos  * usage --
    128       1.3  christos  *	Print a usage message and exit
    129       1.3  christos  */
    130       1.3  christos static void
    131      1.10     perry usage(void)
    132       1.1     perry {
    133       1.8       cgd 
    134       1.3  christos 	(void) fprintf(stderr,
    135      1.15      jmmv     "usage: %s [-0] [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
    136       1.8       cgd 		getprogname());
    137       1.3  christos 	exit(1);
    138       1.1     perry }
    139       1.1     perry 
    140       1.3  christos 
    141       1.3  christos /*
    142       1.3  christos  * get_lines --
    143       1.3  christos  *	Return an array of lines read from input
    144       1.3  christos  */
    145       1.3  christos static void
    146      1.10     perry get_lines(const char *fname, char ***linesp, size_t *nlinesp)
    147       1.1     perry {
    148       1.3  christos 	FILE *fp;
    149       1.3  christos 	char *line;
    150       1.9   mycroft 	size_t size, nlines = 0, maxlines = 128;
    151       1.3  christos 	char **lines = emalloc(sizeof(char *) * maxlines);
    152       1.3  christos 
    153       1.3  christos 	if (strcmp(fname, "-") == 0)
    154       1.3  christos 		fp = stdin;
    155       1.3  christos 	else
    156       1.3  christos 		if ((fp = fopen(fname, "r")) == NULL)
    157       1.3  christos 			err(1, "Cannot open `%s'", fname);
    158       1.3  christos 
    159       1.3  christos 	while ((line = fgetln(fp, &size)) != NULL) {
    160       1.3  christos 		if (size > 0 && line[size - 1] == '\n')
    161       1.3  christos 			size--;
    162       1.3  christos 		lines[nlines] = emalloc(size + 1);
    163       1.3  christos 		(void)memcpy(lines[nlines], line, size);
    164       1.3  christos 		lines[nlines++][size] = '\0';
    165       1.3  christos 		if (nlines >= maxlines) {
    166       1.9   mycroft 			maxlines *= 2;
    167       1.3  christos 			lines = erealloc(lines, (sizeof(char *) * maxlines));
    168       1.1     perry 		}
    169       1.1     perry 	}
    170       1.3  christos 	lines[nlines] = NULL;
    171       1.1     perry 
    172       1.3  christos 	*linesp = lines;
    173       1.3  christos 	*nlinesp = nlines;
    174       1.3  christos 	if (strcmp(fname, "-") != 0)
    175       1.3  christos 		(void)fclose(fp);
    176       1.3  christos }
    177       1.1     perry 
    178       1.3  christos /*
    179       1.3  christos  * get_number --
    180       1.3  christos  *	Return a number or exit on error
    181       1.3  christos  */
    182       1.3  christos static size_t
    183      1.10     perry get_number(const char *str, int ch)
    184       1.3  christos {
    185       1.3  christos 	char *estr;
    186      1.12     lukem 	long number;
    187       1.3  christos 
    188      1.12     lukem 	errno = 0;
    189      1.12     lukem 	number = strtol(str, &estr, 0);
    190       1.3  christos 	if ((number == LONG_MIN || number == LONG_MAX) && errno == ERANGE)
    191       1.3  christos 		err(1, "bad -%c argument `%s'", ch, str);
    192       1.3  christos 	if (*estr)
    193       1.3  christos 		errx(1, "non numeric -%c argument `%s'", ch, str);
    194       1.3  christos 	if (number < 0)
    195       1.3  christos 		errx(1, "negative -%c argument `%s'", ch, str);
    196       1.3  christos 	return (size_t) number;
    197       1.1     perry }
    198       1.1     perry 
    199       1.1     perry int
    200      1.10     perry main(int argc, char *argv[])
    201       1.1     perry {
    202       1.3  christos 	int i, nflag = 0, pflag = 0, ch;
    203       1.3  christos 	char *fname = NULL;
    204       1.6      fair 	size_t *shuffle = NULL;
    205       1.1     perry 	struct timeval tv;
    206       1.3  christos 	char **lines = NULL;
    207       1.3  christos 	size_t nlines = 0, pick = 0;
    208      1.13  christos 	char sep = '\n';
    209       1.2     perry 
    210  1.15.2.1      tron 	while ((ch = getopt(argc, argv, "0f:n:p:")) != -1) {
    211       1.1     perry 		switch(ch) {
    212      1.13  christos 		case '0':
    213      1.13  christos 			sep = '\0';
    214      1.13  christos 			break;
    215       1.3  christos 		case 'f':
    216       1.3  christos 			fname = optarg;
    217       1.1     perry 			break;
    218       1.1     perry 		case 'n':
    219       1.3  christos 			nlines = get_number(optarg, ch);
    220       1.1     perry 			nflag = 1;
    221       1.1     perry 			break;
    222       1.1     perry 		case 'p':
    223       1.3  christos 			pick = get_number(optarg, ch);
    224       1.1     perry 			pflag = 1;
    225       1.1     perry 			break;
    226       1.1     perry 		case '?':
    227       1.1     perry 		default:
    228       1.1     perry 			usage();
    229       1.1     perry 		}
    230       1.1     perry 	}
    231       1.1     perry 	argc -= optind;
    232       1.1     perry 	argv += optind;
    233       1.1     perry 
    234       1.3  christos 	if ((fname && nflag) || (nflag && (argc > 0)))
    235       1.1     perry 		usage();
    236       1.1     perry 
    237       1.3  christos 	if (fname != NULL)
    238       1.3  christos 		get_lines(fname, &lines, &nlines);
    239       1.3  christos 	else if (nflag == 0) {
    240       1.3  christos 		lines = argv;
    241       1.3  christos 		nlines = argc;
    242       1.1     perry 	}
    243       1.1     perry 
    244       1.1     perry 	gettimeofday(&tv, NULL);
    245       1.1     perry 	srandom(getpid() ^ ~getuid() ^ tv.tv_sec ^ tv.tv_usec);
    246       1.5     perry 	if (nlines > 0)
    247       1.5     perry 		shuffle = get_shuffle(nlines);
    248       1.1     perry 
    249       1.1     perry 	if (pflag) {
    250       1.3  christos 		if (pick > nlines)
    251       1.1     perry 			errx(1, "-p specified more components than exist.");
    252       1.3  christos 		nlines = pick;
    253       1.1     perry 	}
    254       1.1     perry 
    255       1.3  christos 	for (i = 0; i < nlines; i++) {
    256       1.1     perry 		if (nflag)
    257      1.14  christos 			printf("%ld", (long)shuffle[i]);
    258       1.1     perry 		else
    259      1.14  christos 			printf("%s", lines[shuffle[i]]);
    260      1.14  christos 		putc(sep, stdout);
    261       1.1     perry 	}
    262       1.1     perry 
    263       1.3  christos 	return 0;
    264       1.1     perry }
    265