root/user/testbin/randcall/main.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. randchar
  2. fillrand
  3. randptr
  4. randint
  5. randoff
  6. randsize
  7. usage
  8. main

   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 
  30 #include <stdlib.h>
  31 #include <stdio.h>
  32 #include <string.h>
  33 
  34 #include "extern.h"
  35 
  36 static
  37 void
  38 randchar(char *c)
  39 {
  40 #if RAND_MAX != 0x7fffffff
  41 #error "This code assumes RAND_MAX is 0x7fffffff"
  42 #endif
  43 
  44         static long lbits = 0;
  45         static long lnum = 0;
  46 
  47         long bit;
  48         int ct = 0;
  49 
  50         *c = 0;
  51 
  52         while (ct < CHAR_BIT) {
  53                 if (lnum==0) {
  54                         lbits = random();
  55                         lnum = 31;
  56                 }
  57 
  58                 bit = lbits & 1;
  59                 if (bit) {
  60                         (*c) |= 1;
  61                 }
  62                 (*c) <<= 1;
  63                 ct++;
  64                 lbits >>= 1;
  65                 lnum--;
  66         }
  67 }
  68 
  69 static
  70 void
  71 fillrand(void *p, size_t len)
  72 {
  73         size_t i;
  74         char *cp = p;
  75         for (i=0; i<len; i++) {
  76                 randchar(&cp[i]);
  77         }
  78 }
  79 
  80 void *
  81 randptr(void)
  82 {
  83         void *x;
  84         fillrand(&x, sizeof(x));
  85         return x;
  86 }
  87 
  88 int
  89 randint(void)
  90 {
  91         int x;
  92         fillrand(&x, sizeof(x));
  93         return x;
  94 }
  95 
  96 off_t
  97 randoff(void)
  98 {
  99         off_t x;
 100         fillrand(&x, sizeof(x));
 101         return x;
 102 }
 103 
 104 size_t
 105 randsize(void)
 106 {
 107         size_t x;
 108         fillrand(&x, sizeof(x));
 109         return x;
 110 }
 111 
 112 static
 113 void
 114 usage(void)
 115 {
 116         printf("Usage: randcall [-f] [-c count] [-r seed] 2|3|4|all\n");
 117         printf("   -f   suppress forking\n");
 118         printf("   -c   set iteration count (default 100)\n");
 119         printf("   -r   set pseudorandom seed (default 0)\n");
 120         exit(1);
 121 }
 122 
 123 int
 124 main(int argc, char *argv[])
 125 {
 126         int count=100, seed = 0, dofork = 1;
 127         int an, i;
 128 
 129         for (i=1; i<argc; i++) {
 130                 if (!strcmp(argv[i], "-f")) {
 131                         dofork = 0;
 132                 }
 133                 else if (!strcmp(argv[i], "-c") && i<argc-1) {
 134                         count = atoi(argv[++i]);
 135                 }
 136                 else if (!strcmp(argv[i], "-r") && i<argc-1) {
 137                         seed = atoi(argv[++i]);
 138                 }
 139                 else if (argv[i][0] == '-') {
 140                         usage();
 141                 }
 142                 else {
 143                         break;
 144                 }
 145         }
 146         if (i != argc-1) {
 147                 usage();
 148         }
 149 
 150         if (!strcmp(argv[i], "all")) {
 151                 an = 5;
 152         }
 153         else {
 154                 an = atoi(argv[i]);
 155                 if (an <2 || an > 4) {
 156                         usage();
 157                 }
 158         }
 159 
 160         printf("Seed: %d  Count: %d\n", seed, count);
 161 
 162         srandom(seed);
 163         trycalls(an, dofork, count);
 164 
 165         return 0;
 166 }

/* [<][>][^][v][top][bottom][index][help] */