"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/bin/tools/genrandom.c" (7 Sep 2020, 2644 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


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 "genrandom.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 
   13 /*! \file */
   14 #include <config.h>
   15 
   16 #include <isc/commandline.h>
   17 #include <isc/print.h>
   18 #include <isc/stdlib.h>
   19 #include <isc/util.h>
   20 
   21 #include <stdio.h>
   22 #include <string.h>
   23 
   24 const char *program = "genrandom";
   25 
   26 ISC_PLATFORM_NORETURN_PRE static void
   27 usage(void) ISC_PLATFORM_NORETURN_POST;
   28 
   29 static void
   30 usage(void) {
   31     fprintf(stderr, "usage: %s [-n 2..9] k file\n", program);
   32     exit(1);
   33 }
   34 
   35 static void
   36 generate(char *filename, unsigned int bytes) {
   37     FILE *fp;
   38 
   39     fp = fopen(filename, "w");
   40     if (fp == NULL) {
   41         printf("failed to open %s\n", filename);
   42         exit(1);
   43     }
   44 
   45     while (bytes > 0) {
   46 #ifndef HAVE_ARC4RANDOM
   47         unsigned short int x = (rand() & 0xFFFF);
   48 #else
   49         unsigned short int x = (arc4random() & 0xFFFF);
   50 #endif
   51         unsigned char c = x & 0xFF;
   52         if (putc(c, fp) == EOF) {
   53             printf("error writing to %s\n", filename);
   54             exit(1);
   55         }
   56         c = x >> 8;
   57         if (putc(c, fp) == EOF) {
   58             printf("error writing to %s\n", filename);
   59             exit(1);
   60         }
   61         bytes -= 2;
   62     }
   63     fclose(fp);
   64 }
   65 
   66 int
   67 main(int argc, char **argv) {
   68     unsigned int bytes;
   69     unsigned int k;
   70     char *endp;
   71     int c, i, n = 1;
   72     size_t len;
   73     char *name;
   74 
   75     isc_commandline_errprint = false;
   76 
   77     while ((c = isc_commandline_parse(argc, argv, "hn:")) != EOF) {
   78         switch (c) {
   79         case 'n':
   80             n = strtol(isc_commandline_argument, &endp, 10);
   81             if ((*endp != 0) || (n <= 1) || (n > 9))
   82                 usage();
   83             break;
   84 
   85         case '?':
   86             if (isc_commandline_option != '?')
   87                 fprintf(stderr, "%s: invalid argument -%c\n",
   88                     program, isc_commandline_option);
   89             /* FALLTHROUGH */
   90         case 'h':
   91             usage();
   92 
   93         default:
   94             fprintf(stderr, "%s: unhandled option -%c\n",
   95                 program, isc_commandline_option);
   96             exit(1);
   97         }
   98     }
   99 
  100     if (isc_commandline_index + 2 != argc)
  101         usage();
  102 
  103     k = strtoul(argv[isc_commandline_index++], &endp, 10);
  104     if (*endp != 0)
  105         usage();
  106     bytes = k << 10;
  107 
  108 #ifndef HAVE_ARC4RANDOM
  109     srand(0x12345678);
  110 #endif
  111     if (n == 1) {
  112         generate(argv[isc_commandline_index], bytes);
  113         return (0);
  114     }
  115 
  116     len = strlen(argv[isc_commandline_index]);
  117     INSIST((len + 2) > len);
  118     len += 2;
  119     name = (char *) malloc(len);
  120     if (name == NULL) {
  121         perror("malloc");
  122         exit(1);
  123     }
  124 
  125     for (i = 1; i <= n; i++) {
  126         snprintf(name, len, "%s%d", argv[isc_commandline_index], i);
  127         generate(name, bytes);
  128     }
  129     free(name);
  130 
  131     return (0);
  132 }