/contrib/bind9/bin/tools/genrandom.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 136 lines · 97 code · 21 blank · 18 comment · 25 complexity · 4483c12bc238799bd2a75782cd0383f3 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009, 2010 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. /* $Id: genrandom.c,v 1.7 2010/05/17 23:51:04 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <isc/commandline.h>
  21. #include <isc/print.h>
  22. #include <isc/stdlib.h>
  23. #include <isc/util.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. const char *program = "genrandom";
  27. ISC_PLATFORM_NORETURN_PRE static void
  28. usage(void) ISC_PLATFORM_NORETURN_POST;
  29. static void
  30. usage(void) {
  31. fprintf(stderr, "usage: %s [-n 2..9] k file\n", program);
  32. exit(1);
  33. }
  34. static void
  35. generate(char *filename, unsigned int bytes) {
  36. FILE *fp;
  37. fp = fopen(filename, "w");
  38. if (fp == NULL) {
  39. printf("failed to open %s\n", filename);
  40. exit(1);
  41. }
  42. while (bytes > 0) {
  43. #ifndef HAVE_ARC4RANDOM
  44. unsigned short int x = (rand() & 0xFFFF);
  45. #else
  46. unsigned short int x = (arc4random() & 0xFFFF);
  47. #endif
  48. unsigned char c = x & 0xFF;
  49. if (putc(c, fp) == EOF) {
  50. printf("error writing to %s\n", filename);
  51. exit(1);
  52. }
  53. c = x >> 8;
  54. if (putc(c, fp) == EOF) {
  55. printf("error writing to %s\n", filename);
  56. exit(1);
  57. }
  58. bytes -= 2;
  59. }
  60. fclose(fp);
  61. }
  62. int
  63. main(int argc, char **argv) {
  64. unsigned int bytes;
  65. unsigned int k;
  66. char *endp;
  67. int c, i, n = 1;
  68. size_t len;
  69. char *name;
  70. isc_commandline_errprint = ISC_FALSE;
  71. while ((c = isc_commandline_parse(argc, argv, "hn:")) != EOF) {
  72. switch (c) {
  73. case 'n':
  74. n = strtol(isc_commandline_argument, &endp, 10);
  75. if ((*endp != 0) || (n <= 1) || (n > 9))
  76. usage();
  77. break;
  78. case '?':
  79. if (isc_commandline_option != '?')
  80. fprintf(stderr, "%s: invalid argument -%c\n",
  81. program, isc_commandline_option);
  82. case 'h':
  83. usage();
  84. default:
  85. fprintf(stderr, "%s: unhandled option -%c\n",
  86. program, isc_commandline_option);
  87. exit(1);
  88. }
  89. }
  90. if (isc_commandline_index + 2 != argc)
  91. usage();
  92. k = strtoul(argv[isc_commandline_index++], &endp, 10);
  93. if (*endp != 0)
  94. usage();
  95. bytes = k << 10;
  96. #ifndef HAVE_ARC4RANDOM
  97. srand(0x12345678);
  98. #endif
  99. if (n == 1) {
  100. generate(argv[isc_commandline_index], bytes);
  101. return (0);
  102. }
  103. len = strlen(argv[isc_commandline_index]) + 2;
  104. name = (char *) malloc(len);
  105. if (name == NULL) {
  106. perror("malloc");
  107. exit(1);
  108. }
  109. for (i = 1; i <= n; i++) {
  110. snprintf(name, len, "%s%d", argv[isc_commandline_index], i);
  111. generate(name, bytes);
  112. }
  113. free(name);
  114. return (0);
  115. }