1/* 2 * Copyright (C) 2004, 2005, 2007, 2009, 2010, 2012 Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (C) 2000-2003 Internet Software Consortium. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 * PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18/* $Id: genrandom.c,v 1.7 2010/05/17 23:51:04 tbox Exp $ */ 19 20/*! \file */ 21#include <config.h> 22 23#include <isc/commandline.h> 24#include <isc/print.h> 25#include <isc/stdlib.h> 26#include <isc/util.h> 27 28#include <stdio.h> 29#include <string.h> 30 31const char *program = "genrandom"; 32 33ISC_PLATFORM_NORETURN_PRE static void 34usage(void) ISC_PLATFORM_NORETURN_POST; 35 36static void 37usage(void) { 38 fprintf(stderr, "usage: %s [-n 2..9] k file\n", program); 39 exit(1); 40} 41 42static void 43generate(char *filename, unsigned int bytes) { 44 FILE *fp; 45 46 fp = fopen(filename, "w"); 47 if (fp == NULL) { 48 printf("failed to open %s\n", filename); 49 exit(1); 50 } 51 52 while (bytes > 0) { 53#ifndef HAVE_ARC4RANDOM 54 unsigned short int x = (rand() & 0xFFFF); 55#else 56 unsigned short int x = (arc4random() & 0xFFFF); 57#endif 58 unsigned char c = x & 0xFF; 59 if (putc(c, fp) == EOF) { 60 printf("error writing to %s\n", filename); 61 exit(1); 62 } 63 c = x >> 8; 64 if (putc(c, fp) == EOF) { 65 printf("error writing to %s\n", filename); 66 exit(1); 67 } 68 bytes -= 2; 69 } 70 fclose(fp); 71} 72 73int 74main(int argc, char **argv) { 75 unsigned int bytes; 76 unsigned int k; 77 char *endp; 78 int c, i, n = 1; 79 size_t len; 80 char *name; 81 82 isc_commandline_errprint = ISC_FALSE; 83 84 while ((c = isc_commandline_parse(argc, argv, "hn:")) != EOF) { 85 switch (c) { 86 case 'n': 87 n = strtol(isc_commandline_argument, &endp, 10); 88 if ((*endp != 0) || (n <= 1) || (n > 9)) 89 usage(); 90 break; 91 92 case '?': 93 if (isc_commandline_option != '?') 94 fprintf(stderr, "%s: invalid argument -%c\n", 95 program, isc_commandline_option); 96 /* FALLTHROUGH */ 97 case 'h': 98 usage(); 99 100 default: 101 fprintf(stderr, "%s: unhandled option -%c\n", 102 program, isc_commandline_option); 103 exit(1); 104 } 105 } 106 107 if (isc_commandline_index + 2 != argc) 108 usage(); 109 110 k = strtoul(argv[isc_commandline_index++], &endp, 10); 111 if (*endp != 0) 112 usage(); 113 bytes = k << 10; 114 115#ifndef HAVE_ARC4RANDOM 116 srand(0x12345678); 117#endif 118 if (n == 1) { 119 generate(argv[isc_commandline_index], bytes); 120 return (0); 121 } 122 123 len = strlen(argv[isc_commandline_index]) + 2; 124 name = (char *) malloc(len); 125 if (name == NULL) { 126 perror("malloc"); 127 exit(1); 128 } 129 130 for (i = 1; i <= n; i++) { 131 snprintf(name, len, "%s%d", argv[isc_commandline_index], i); 132 generate(name, bytes); 133 } 134 free(name); 135 136 return (0); 137} 138