2 * Copyright (C) 2004, 2005, 2007, 2009, 2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
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.
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.
18 /* $Id: genrandom.c,v 1.7 2010-05-17 23:51:04 tbox Exp $ */
23 #include <isc/commandline.h>
24 #include <isc/print.h>
25 #include <isc/stdlib.h>
31 const char *program = "genrandom";
33 ISC_PLATFORM_NORETURN_PRE static void
34 usage(void) ISC_PLATFORM_NORETURN_POST;
38 fprintf(stderr, "usage: %s [-n 2..9] k file\n", program);
43 generate(char *filename, unsigned int bytes) {
46 fp = fopen(filename, "w");
48 printf("failed to open %s\n", filename);
53 #ifndef HAVE_ARC4RANDOM
54 unsigned short int x = (rand() & 0xFFFF);
56 unsigned short int x = (arc4random() & 0xFFFF);
58 unsigned char c = x & 0xFF;
59 if (putc(c, fp) == EOF) {
60 printf("error writing to %s\n", filename);
64 if (putc(c, fp) == EOF) {
65 printf("error writing to %s\n", filename);
74 main(int argc, char **argv) {
82 isc_commandline_errprint = ISC_FALSE;
84 while ((c = isc_commandline_parse(argc, argv, "hn:")) != EOF) {
87 n = strtol(isc_commandline_argument, &endp, 10);
88 if ((*endp != 0) || (n <= 1) || (n > 9))
93 if (isc_commandline_option != '?')
94 fprintf(stderr, "%s: invalid argument -%c\n",
95 program, isc_commandline_option);
100 fprintf(stderr, "%s: unhandled option -%c\n",
101 program, isc_commandline_option);
106 if (isc_commandline_index + 2 != argc)
109 k = strtoul(argv[isc_commandline_index++], &endp, 10);
114 #ifndef HAVE_ARC4RANDOM
118 generate(argv[isc_commandline_index], bytes);
122 len = strlen(argv[isc_commandline_index]) + 2;
123 name = (char *) malloc(len);
129 for (i = 1; i <= n; i++) {
130 snprintf(name, len, "%s%d", argv[isc_commandline_index], i);
131 generate(name, bytes);