genfile.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Generate a file containing some preset patterns.
  2. Copyright (C) 1995, 1996, 1997, 2001, 2003 Free Software
  3. Foundation, Inc.
  4. François Pinard <[email protected]>, 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. 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. /* Main program. Decode ARGC arguments passed through the ARGV array
  67. of strings, then launch execution. */
  68. /* Long options equivalences. */
  69. static const struct option long_options[] =
  70. {
  71. {"help", no_argument, &show_help, 1},
  72. {"length", required_argument, NULL, 'l'},
  73. {"pattern", required_argument, NULL, 'p'},
  74. {"version", no_argument, &show_version, 1},
  75. {0, 0, 0, 0},
  76. };
  77. static char const * const pattern_args[] = { "default", "zeros", 0 };
  78. static enum pattern const pattern_types[] = {DEFAULT_PATTERN, ZEROS_PATTERN};
  79. int
  80. main (int argc, char *const *argv)
  81. {
  82. int option_char; /* option character */
  83. int counter; /* general purpose counter */
  84. /* Decode command options. */
  85. program_name = argv[0];
  86. setlocale (LC_ALL, "");
  87. while (option_char = getopt_long (argc, argv, "l:p:", long_options, NULL),
  88. option_char != EOF)
  89. switch (option_char)
  90. {
  91. default:
  92. usage (EXIT_FAILURE);
  93. case '\0':
  94. break;
  95. case 'l':
  96. file_length = atoi (optarg);
  97. break;
  98. case 'p':
  99. pattern = XARGMATCH ("--pattern", optarg,
  100. pattern_args, pattern_types);
  101. break;
  102. }
  103. /* Process trivial options. */
  104. if (show_version)
  105. {
  106. printf ("genfile (GNU %s) %s\n", PACKAGE, VERSION);
  107. printf (_("Copyright (C) %d Free Software Foundation, Inc.\n"), 2003);
  108. puts (_("\
  109. This program comes with NO WARRANTY, to the extent permitted by law.\n\
  110. You may redistribute it under the terms of the GNU General Public License;\n\
  111. see the file named COPYING for details."));
  112. /* Note to translator: Please translate "F. Pinard" to "François
  113. Pinard" if "ç" (c-with-cedilla) is available in the
  114. translation's character set and encoding. */
  115. puts (_("Written by F. Pinard."));
  116. exit (EXIT_SUCCESS);
  117. }
  118. if (show_help)
  119. usage (EXIT_SUCCESS);
  120. if (optind < argc)
  121. usage (EXIT_FAILURE);
  122. /* Generate file. */
  123. switch (pattern)
  124. {
  125. case DEFAULT_PATTERN:
  126. for (counter = 0; counter < file_length; counter++)
  127. putchar (counter & 255);
  128. break;
  129. case ZEROS_PATTERN:
  130. for (counter = 0; counter < file_length; counter++)
  131. putchar (0);
  132. break;
  133. }
  134. exit (0);
  135. }