genfile.c 4.4 KB

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