From 3ec79bbc03fc5378a6cb37b276807cd40b5332aa Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Mon, 17 May 2010 04:38:45 +0000 Subject: [PATCH] 2895. [func] genrandom: add support for the generation of multiple files. [RT #20917] --- CHANGES | 3 + bin/tools/genrandom.c | 113 +++++++++++++++++++++++++++--------- bin/tools/genrandom.docbook | 19 ++++-- 3 files changed, 104 insertions(+), 31 deletions(-) diff --git a/CHANGES b/CHANGES index 6d51d6d93e..cccd9a5cd6 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +2895. [func] genrandom: add support for the generation of multiple + files. [RT #20917] + 2894. [contrib] DLZ LDAP support now use '$' not '%'. [RT #21294] 2893. [bug] Improve managed keys support. New named.conf option diff --git a/bin/tools/genrandom.c b/bin/tools/genrandom.c index b34416e7d0..d701242a35 100644 --- a/bin/tools/genrandom.c +++ b/bin/tools/genrandom.c @@ -15,43 +15,39 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: genrandom.c,v 1.4 2009/03/07 23:47:45 tbox Exp $ */ +/* $Id: genrandom.c,v 1.5 2010/05/17 04:38:45 marka Exp $ */ /*! \file */ #include -#include -#include - +#include #include +#include -int -main(int argc, char **argv) { - unsigned int bytes; - unsigned int k; - char *endp; +#include +#include + +const char *program = "genrandom"; + +ISC_PLATFORM_NORETURN_PRE static void +usage(void) ISC_PLATFORM_NORETURN_POST; + +static void +usage(void) { + fprintf(stderr, "usage: %s [-n 2..9] k file\n", program); + exit(1); +} + +static void +generate(char *filename, unsigned int bytes) { FILE *fp; - if (argc != 3) { - printf("usage: genrandom k file\n"); - exit(1); - } - k = strtoul(argv[1], &endp, 10); - if (*endp != 0) { - printf("usage: genrandom k file\n"); - exit(1); - } - bytes = k << 10; - - fp = fopen(argv[2], "w"); + fp = fopen(filename, "w"); if (fp == NULL) { - printf("failed to open %s\n", argv[2]); + printf("failed to open %s\n", filename); exit(1); } -#ifndef HAVE_ARC4RANDOM - srand(0x12345678); -#endif while (bytes > 0) { #ifndef HAVE_ARC4RANDOM unsigned short int x = (rand() & 0xFFFF); @@ -60,17 +56,80 @@ main(int argc, char **argv) { #endif unsigned char c = x & 0xFF; if (putc(c, fp) == EOF) { - printf("error writing to file\n"); + printf("error writing to %s\n", filename); exit(1); } c = x >> 8; if (putc(c, fp) == EOF) { - printf("error writing to file\n"); + printf("error writing to %s\n", filename); exit(1); } bytes -= 2; } fclose(fp); +} + +int +main(int argc, char **argv) { + unsigned int bytes; + unsigned int k; + char *endp; + int c, i, n = 1; + size_t len; + char *name; + + isc_commandline_errprint = ISC_FALSE; + + while ((c = isc_commandline_parse(argc, argv, "hn:")) != EOF) { + switch (c) { + case 'n': + n = strtol(isc_commandline_argument, &endp, 10); + if ((*endp != 0) || (n <= 1) || (n > 9)) + usage(); + break; + + case '?': + if (isc_commandline_option != '?') + fprintf(stderr, "%s: invalid argument -%c\n", + program, isc_commandline_option); + case 'h': + usage(); + + default: + fprintf(stderr, "%s: unhandled option -%c\n", + program, isc_commandline_option); + exit(1); + } + } + + if (isc_commandline_index + 2 != argc) + usage(); + + k = strtoul(argv[isc_commandline_index++], &endp, 10); + if (*endp != 0) + usage(); + bytes = k << 10; + +#ifndef HAVE_ARC4RANDOM + srand(0x12345678); +#endif + if (n == 1) { + generate(argv[isc_commandline_index], bytes); + return (0); + } + + len = strlen(argv[isc_commandline_index]) + 2; + name = (char *) malloc(len); + if (name == NULL) { + perror("malloc"); + exit(1); + } + + for (i = 1; i <= n; i++) { + snprintf(name, len, "%s%d", argv[isc_commandline_index], i); + generate(name, bytes); + } + free(name); return (0); } diff --git a/bin/tools/genrandom.docbook b/bin/tools/genrandom.docbook index 581c93679d..48c8cd75cb 100644 --- a/bin/tools/genrandom.docbook +++ b/bin/tools/genrandom.docbook @@ -17,7 +17,7 @@ - PERFORMANCE OF THIS SOFTWARE. --> - + Feb 19, 2009 @@ -44,6 +44,7 @@ genrandom + size filename @@ -53,15 +54,25 @@ DESCRIPTION genrandom - generates a file containing a specified quantity of pseudo-random - data, which can be used as a source of entropy for other commands - on systems with no random device. + generates a file or a set of files containing a specified quantity + of pseudo-random data, which can be used as a source of entropy for + other commands on systems with no random device. ARGUMENTS + + -n number + + + In place of generating one file, generates + (from 2 to 9) files, appending to the name. + + + + size