"Fossies" - the Fresh Open Source Software Archive

Member "bonnie++-2.00a/generate_randfile.cpp" (3 Jul 2009, 1129 Bytes) of package /linux/privat/bonnie++-2.00a.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "generate_randfile.cpp" see the Fossies "Dox" file reference documentation.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <unistd.h>
    4 #include <time.h>
    5 #include <netinet/in.h>
    6 
    7 void usage()
    8 {
    9   fprintf(stderr, "Usage: generate_randfile [-s seed] [-f file] count\n");
   10   exit(1);
   11 }
   12 
   13 int main(int argc, char **argv)
   14 {
   15   if(argc < 2)
   16   {
   17     usage();
   18   }
   19   unsigned int seed = getpid() ^ time(NULL);
   20   FILE *fp = stdout;
   21   int int_c;
   22   while(-1 != (int_c = getopt(argc, argv, "s:f:")) )
   23   {
   24     switch(char(int_c))
   25     {
   26       case '?':
   27       case ':':
   28         usage();
   29       break;
   30       case 's':
   31         if(sscanf(optarg, "%u", &seed) != 1)
   32           usage();
   33       break;
   34       case 'f':
   35         fp = fopen(optarg, "w");
   36         if(fp == NULL)
   37           usage();
   38       break;
   39     }
   40   }
   41   if(optind >= argc)
   42     usage();
   43   int count = atoi(argv[optind]);
   44   srand(seed);
   45   fprintf(stderr, "Generating %d random numbers with seed %d.\n", count, seed);
   46   for(int i = 0; i < count; i++)
   47   {
   48     unsigned long val = htonl(rand());
   49     if(fwrite(&val, sizeof(val), 1, fp) != 1)
   50     {
   51       fprintf(stderr, "Can't write item %d.\n", i);
   52       return 1;
   53     }
   54   }
   55   if(fp != stdout)
   56     fclose(fp);
   57   return 0;
   58 }