random.c 4.2 KB

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