home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / gnu / tar.txt / tar-1.12 / tests / genfile.c < prev    next >
C/C++ Source or Header  |  1997-04-25  |  5KB  |  195 lines

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