genfile.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Generate a file containing some preset patterns.
  2. Copyright © 1995, 1996, 1997 Free Software Foundation, Inc.
  3. François Pinard <[email protected]>, 1995.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "system.h"
  17. #include <getopt.h>
  18. #ifndef EXIT_SUCCESS
  19. # define EXIT_SUCCESS 0
  20. #endif
  21. #ifndef EXIT_FAILURE
  22. # define EXIT_FAILURE 1
  23. #endif
  24. enum pattern
  25. {
  26. DEFAULT,
  27. ZEROS
  28. };
  29. /* The name this program was run with. */
  30. const char *program_name;
  31. /* If nonzero, display usage information and exit. */
  32. static int show_help = 0;
  33. /* If nonzero, print the version on standard output and exit. */
  34. static int show_version = 0;
  35. /* Length of file to generate. */
  36. static int file_length = 0;
  37. /* Pattern to generate. */
  38. static enum pattern pattern = DEFAULT;
  39. /*-----------------------------------------------.
  40. | Explain how to use the program, then get out. |
  41. `-----------------------------------------------*/
  42. void
  43. usage (int status)
  44. {
  45. if (status != EXIT_SUCCESS)
  46. fprintf (stderr, _("Try `%s --help' for more information.\n"),
  47. program_name);
  48. else
  49. {
  50. printf (_("Generate data files for GNU tar test suite.\n"));
  51. printf (_("\
  52. \n\
  53. Usage: %s [OPTION]...\n"), program_name);
  54. fputs (_("\
  55. If a long option shows an argument as mandatory, then it is mandatory\n\
  56. for the equivalent short option also.\n\
  57. \n\
  58. -l, --file-length=LENGTH LENGTH of generated file\n\
  59. -p, --pattern=PATTERN PATTERN is `default' or `zeros'\n\
  60. --help display this help and exit\n\
  61. --version output version information and exit\n"),
  62. stdout);
  63. }
  64. exit (status);
  65. }
  66. /*----------------------------------------------------------------------.
  67. | Main program. Decode ARGC arguments passed through the ARGV array of |
  68. | strings, then launch execution. |
  69. `----------------------------------------------------------------------*/
  70. /* Long options equivalences. */
  71. static const struct option long_options[] =
  72. {
  73. {"help", no_argument, &show_help, 1},
  74. {"length", required_argument, NULL, 'l'},
  75. {"pattern", required_argument, NULL, 'p'},
  76. {"version", no_argument, &show_version, 1},
  77. {0, 0, 0, 0},
  78. };
  79. const char *pattern_strings[] =
  80. {
  81. "default", /* 0 */
  82. "zeros", /* 1 */
  83. NULL
  84. };
  85. int
  86. main (int argc, char *const *argv)
  87. {
  88. int option_char; /* option character */
  89. int counter; /* general purpose counter */
  90. /* Decode command options. */
  91. program_name = argv[0];
  92. setlocale (LC_ALL, "");
  93. while (option_char = getopt_long (argc, argv, "l:p:", long_options, NULL),
  94. option_char != EOF)
  95. switch (option_char)
  96. {
  97. default:
  98. usage (EXIT_FAILURE);
  99. case '\0':
  100. break;
  101. case 'l':
  102. file_length = atoi (optarg);
  103. break;
  104. case 'p':
  105. switch (argmatch (optarg, pattern_strings))
  106. {
  107. case -2:
  108. error (0, 0, _("Ambiguous pattern `%s'"), optarg);
  109. usage (EXIT_FAILURE);
  110. case -1:
  111. error (0, 0, _("Unknown pattern `%s'"), optarg);
  112. usage (EXIT_FAILURE);
  113. case 0:
  114. pattern = DEFAULT;
  115. break;
  116. case 1:
  117. pattern = ZEROS;
  118. break;
  119. }
  120. break;
  121. }
  122. /* Process trivial options. */
  123. if (show_version)
  124. {
  125. printf ("genfile (GNU %s) %s\n", PACKAGE, VERSION);
  126. fputs (_("\
  127. \n\
  128. Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.\n"),
  129. stdout);
  130. fputs (_("\
  131. This is free software; see the source for copying conditions. There is NO\n\
  132. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"),
  133. stdout);
  134. fputs (_("\
  135. \n\
  136. Written by François Pinard <[email protected]>.\n"),
  137. stdout);
  138. exit (EXIT_SUCCESS);
  139. }
  140. if (show_help)
  141. usage (EXIT_SUCCESS);
  142. if (optind < argc)
  143. usage (EXIT_FAILURE);
  144. /* Generate file. */
  145. switch (pattern)
  146. {
  147. case DEFAULT:
  148. for (counter = 0; counter < file_length; counter++)
  149. putchar (counter & 255);
  150. break;
  151. case ZEROS:
  152. for (counter = 0; counter < file_length; counter++)
  153. putchar (0);
  154. break;
  155. }
  156. exit (0);
  157. }