os161-1.99
 All Data Structures
main.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <string.h>
00033 
00034 #include "extern.h"
00035 
00036 static
00037 void
00038 randchar(char *c)
00039 {
00040 #if RAND_MAX != 0x7fffffff
00041 #error "This code assumes RAND_MAX is 0x7fffffff"
00042 #endif
00043 
00044         static long lbits = 0;
00045         static long lnum = 0;
00046 
00047         long bit;
00048         int ct = 0;
00049 
00050         *c = 0;
00051 
00052         while (ct < CHAR_BIT) {
00053                 if (lnum==0) {
00054                         lbits = random();
00055                         lnum = 31;
00056                 }
00057 
00058                 bit = lbits & 1;
00059                 if (bit) {
00060                         (*c) |= 1;
00061                 }
00062                 (*c) <<= 1;
00063                 ct++;
00064                 lbits >>= 1;
00065                 lnum--;
00066         }
00067 }
00068 
00069 static
00070 void
00071 fillrand(void *p, size_t len)
00072 {
00073         size_t i;
00074         char *cp = p;
00075         for (i=0; i<len; i++) {
00076                 randchar(&cp[i]);
00077         }
00078 }
00079 
00080 void *
00081 randptr(void)
00082 {
00083         void *x;
00084         fillrand(&x, sizeof(x));
00085         return x;
00086 }
00087 
00088 int
00089 randint(void)
00090 {
00091         int x;
00092         fillrand(&x, sizeof(x));
00093         return x;
00094 }
00095 
00096 off_t
00097 randoff(void)
00098 {
00099         off_t x;
00100         fillrand(&x, sizeof(x));
00101         return x;
00102 }
00103 
00104 size_t
00105 randsize(void)
00106 {
00107         size_t x;
00108         fillrand(&x, sizeof(x));
00109         return x;
00110 }
00111 
00112 static
00113 void
00114 usage(void)
00115 {
00116         printf("Usage: randcall [-f] [-c count] [-r seed] 2|3|4|all\n");
00117         printf("   -f   suppress forking\n");
00118         printf("   -c   set iteration count (default 100)\n");
00119         printf("   -r   set pseudorandom seed (default 0)\n");
00120         exit(1);
00121 }
00122 
00123 int
00124 main(int argc, char *argv[])
00125 {
00126         int count=100, seed = 0, dofork = 1;
00127         int an, i;
00128 
00129         for (i=1; i<argc; i++) {
00130                 if (!strcmp(argv[i], "-f")) {
00131                         dofork = 0;
00132                 }
00133                 else if (!strcmp(argv[i], "-c") && i<argc-1) {
00134                         count = atoi(argv[++i]);
00135                 }
00136                 else if (!strcmp(argv[i], "-r") && i<argc-1) {
00137                         seed = atoi(argv[++i]);
00138                 }
00139                 else if (argv[i][0] == '-') {
00140                         usage();
00141                 }
00142                 else {
00143                         break;
00144                 }
00145         }
00146         if (i != argc-1) {
00147                 usage();
00148         }
00149 
00150         if (!strcmp(argv[i], "all")) {
00151                 an = 5;
00152         }
00153         else {
00154                 an = atoi(argv[i]);
00155                 if (an <2 || an > 4) {
00156                         usage();
00157                 }
00158         }
00159 
00160         printf("Seed: %d  Count: %d\n", seed, count);
00161 
00162         srandom(seed);
00163         trycalls(an, dofork, count);
00164 
00165         return 0;
00166 }
 All Data Structures