Home | History | Annotate | Line # | Download | only in random
random.c revision 1.8
      1 /*	$NetBSD: random.c,v 1.8 2003/08/07 09:37:35 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Guy Harris at Network Appliance Corp.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1994\n\
     38 	The Regents of the University of California.  All rights reserved.\n");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)random.c	8.6 (Berkeley) 6/1/94";
     44 #else
     45 __RCSID("$NetBSD: random.c,v 1.8 2003/08/07 09:37:35 agc Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/time.h>
     51 
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <time.h>
     57 #include <unistd.h>
     58 #include <limits.h>
     59 
     60 #define MAXRANDOM	2147483647
     61 
     62 int  main __P((int, char **));
     63 void usage __P((void)) __attribute__((__noreturn__));
     64 
     65 int
     66 main(argc, argv)
     67 	int argc;
     68 	char *argv[];
     69 {
     70 	struct timeval tp;
     71 	double denom;
     72 	int ch, random_exit, selected, unbuffer_output;
     73 	char *ep;
     74 
     75 	denom = 0;
     76 	random_exit = unbuffer_output = 0;
     77 	while ((ch = getopt(argc, argv, "er")) != -1)
     78 		switch (ch) {
     79 		case 'e':
     80 			random_exit = 1;
     81 			break;
     82 		case 'r':
     83 			unbuffer_output = 1;
     84 			break;
     85 		default:
     86 		case '?':
     87 			usage();
     88 			/* NOTREACHED */
     89 		}
     90 
     91 	argc -= optind;
     92 	argv += optind;
     93 
     94 	switch (argc) {
     95 	case 0:
     96 		denom = 2;
     97 		break;
     98 	case 1:
     99 		errno = 0;
    100 		denom = strtod(*argv, &ep);
    101 		if (errno == ERANGE)
    102 			err(1, "%s", *argv);
    103 		if (denom == 0 || *ep != '\0')
    104 			errx(1, "denominator is not valid.");
    105 		break;
    106 	default:
    107 		usage();
    108 		/* NOTREACHED */
    109 	}
    110 
    111 	(void)gettimeofday(&tp, NULL);
    112 	srandom((u_int)(tp.tv_usec + tp.tv_sec + getpid()));
    113 
    114 	/* Compute a random exit status between 0 and denom - 1. */
    115 	if (random_exit)
    116 		return ((denom * random()) / MAXRANDOM);
    117 
    118 	/*
    119 	 * Act as a filter, randomly choosing lines of the standard input
    120 	 * to write to the standard output.
    121 	 */
    122 	if (unbuffer_output)
    123 		setbuf(stdout, NULL);
    124 
    125 	/*
    126 	 * Select whether to print the first line.  (Prime the pump.)
    127 	 * We find a random number between 0 and denom - 1 and, if it's
    128 	 * 0 (which has a 1 / denom chance of being true), we select the
    129 	 * line.
    130 	 */
    131 	selected = (int)(denom * random() / MAXRANDOM) == 0;
    132 	while ((ch = getchar()) != EOF) {
    133 		if (selected)
    134 			(void)putchar(ch);
    135 		if (ch == '\n') {
    136 			/* End of that line.  See if we got an error. */
    137 			if (ferror(stdout))
    138 				err(2, "stdout");
    139 
    140 			/* Now see if the next line is to be printed. */
    141 			selected = (int)(denom * random() / MAXRANDOM) == 0;
    142 		}
    143 	}
    144 	if (ferror(stdin))
    145 		err(2, "stdin");
    146 	exit (0);
    147 }
    148 
    149 void
    150 usage()
    151 {
    152 
    153 	(void)fprintf(stderr, "usage: random [-er] [denominator]\n");
    154 	exit(1);
    155 }
    156