main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include "extern.h"
  33. static
  34. void
  35. randchar(char *c)
  36. {
  37. #if RAND_MAX != 0x7fffffff
  38. #error "This code assumes RAND_MAX is 0x7fffffff"
  39. #endif
  40. static long lbits = 0;
  41. static long lnum = 0;
  42. long bit;
  43. int ct = 0;
  44. *c = 0;
  45. while (ct < CHAR_BIT) {
  46. if (lnum==0) {
  47. lbits = random();
  48. lnum = 31;
  49. }
  50. bit = lbits & 1;
  51. if (bit) {
  52. (*c) |= 1;
  53. }
  54. (*c) <<= 1;
  55. ct++;
  56. lbits >>= 1;
  57. lnum--;
  58. }
  59. }
  60. static
  61. void
  62. fillrand(void *p, size_t len)
  63. {
  64. size_t i;
  65. char *cp = p;
  66. for (i=0; i<len; i++) {
  67. randchar(&cp[i]);
  68. }
  69. }
  70. void *
  71. randptr(void)
  72. {
  73. void *x;
  74. fillrand(&x, sizeof(x));
  75. return x;
  76. }
  77. int
  78. randint(void)
  79. {
  80. int x;
  81. fillrand(&x, sizeof(x));
  82. return x;
  83. }
  84. off_t
  85. randoff(void)
  86. {
  87. off_t x;
  88. fillrand(&x, sizeof(x));
  89. return x;
  90. }
  91. size_t
  92. randsize(void)
  93. {
  94. size_t x;
  95. fillrand(&x, sizeof(x));
  96. return x;
  97. }
  98. static
  99. void
  100. usage(void)
  101. {
  102. printf("Usage: randcall [-f] [-c count] [-r seed] 2|3|4|all\n");
  103. printf(" -f suppress forking\n");
  104. printf(" -c set iteration count (default 100)\n");
  105. printf(" -r set pseudorandom seed (default 0)\n");
  106. exit(1);
  107. }
  108. int
  109. main(int argc, char *argv[])
  110. {
  111. int count=100, seed = 0, dofork = 1;
  112. int an, i;
  113. for (i=1; i<argc; i++) {
  114. if (!strcmp(argv[i], "-f")) {
  115. dofork = 0;
  116. }
  117. else if (!strcmp(argv[i], "-c") && i<argc-1) {
  118. count = atoi(argv[++i]);
  119. }
  120. else if (!strcmp(argv[i], "-r") && i<argc-1) {
  121. seed = atoi(argv[++i]);
  122. }
  123. else if (argv[i][0] == '-') {
  124. usage();
  125. }
  126. else {
  127. break;
  128. }
  129. }
  130. if (i != argc-1) {
  131. usage();
  132. }
  133. if (!strcmp(argv[i], "all")) {
  134. an = 5;
  135. }
  136. else {
  137. an = atoi(argv[i]);
  138. if (an <2 || an > 4) {
  139. usage();
  140. }
  141. }
  142. printf("Seed: %d Count: %d\n", seed, count);
  143. srandom(seed);
  144. trycalls(an, dofork, count);
  145. return 0;
  146. }