/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <stdlib.h>30#include <stdio.h>31#include <string.h>3233#include "extern.h"3435static36void37randchar(char *c)38{39#if RAND_MAX != 0x7fffffff40#error "This code assumes RAND_MAX is 0x7fffffff"41#endif4243static long lbits = 0;44static long lnum = 0;4546long bit;47int ct = 0;4849*c = 0;5051while (ct < CHAR_BIT) {52if (lnum==0) {53lbits = random();54lnum = 31;55}5657bit = lbits & 1;58if (bit) {59(*c) |= 1;60}61(*c) <<= 1;62ct++;63lbits >>= 1;64lnum--;65}66}6768static69void70fillrand(void *p, size_t len)71{72size_t i;73char *cp = p;74for (i=0; i<len; i++) {75randchar(&cp[i]);76}77}7879void *80randptr(void)81{82void *x;83fillrand(&x, sizeof(x));84return x;85}8687int88randint(void)89{90int x;91fillrand(&x, sizeof(x));92return x;93}9495off_t96randoff(void)97{98off_t x;99fillrand(&x, sizeof(x));100return x;101}102103size_t104randsize(void)105{106size_t x;107fillrand(&x, sizeof(x));108return x;109}110111static112void113usage(void)114{115printf("Usage: randcall [-f] [-c count] [-r seed] 2|3|4|all\n");116printf(" -f suppress forking\n");117printf(" -c set iteration count (default 100)\n");118printf(" -r set pseudorandom seed (default 0)\n");119exit(1);120}121122int123main(int argc, char *argv[])124{125int count=100, seed = 0, dofork = 1;126int an, i;127128for (i=1; i<argc; i++) {129if (!strcmp(argv[i], "-f")) {130dofork = 0;131}132else if (!strcmp(argv[i], "-c") && i<argc-1) {133count = atoi(argv[++i]);134}135else if (!strcmp(argv[i], "-r") && i<argc-1) {136seed = atoi(argv[++i]);137}138else if (argv[i][0] == '-') {139usage();140}141else {142break;143}144}145if (i != argc-1) {146usage();147}148149if (!strcmp(argv[i], "all")) {150an = 5;151}152else {153an = atoi(argv[i]);154if (an <2 || an > 4) {155usage();156}157}158159printf("Seed: %d Count: %d\n", seed, count);160161srandom(seed);162trycalls(an, dofork, count);163164return 0;165}166167168