home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / src / genfile.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  128 lines

  1. /* Generate a file containing some preset patterns.
  2.    Copyright (C) 1995 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
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include "system.h"
  21.  
  22. #ifndef EXIT_SUCCESS
  23. # define EXIT_SUCCESS 0
  24. #endif
  25. #ifndef EXIT_FAILURE
  26. # define EXIT_FAILURE 1
  27. #endif
  28.  
  29. /* The name this program was run with. */
  30. const char *program_name;
  31.  
  32. /* If non-zero, display usage information and exit.  */
  33. static int show_help = 0;
  34.  
  35. /* If non-zero, print the version on standard output and exit.  */
  36. static int show_version = 0;
  37.  
  38. /* Length of file to generate.  */
  39. static int file_length = 0;
  40.  
  41. /*-----------------------------------------------.
  42. | Explain how to use the program, then get out.     |
  43. `-----------------------------------------------*/
  44.  
  45. void
  46. usage (int status)
  47. {
  48.   if (status != EXIT_SUCCESS)
  49.     fprintf (stderr, _("Try `%s --help' for more information.\n"),
  50.          program_name);
  51.   else
  52.     {
  53.       printf ("GNU %s %s\n", PACKAGE, VERSION);
  54.       printf (_("\
  55. \n\
  56. Usage: %s [OPTION]...\n"), program_name);
  57.       fputs (_("\
  58. Mandatory arguments to long options are mandatory for short options too.\n\
  59. \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. /*----------------------------------------------------------------------.
  68. | Main program.  Decode ARGC arguments passed through the ARGV array of |
  69. | strings, then launch execution.                        |
  70. `----------------------------------------------------------------------*/
  71.  
  72. /* Long options equivalences.  */
  73. static const struct option long_options[] =
  74. {
  75.   {"help", no_argument, &show_help, 1},
  76.   {"length", required_argument, NULL, 'l'},
  77.   {"version", no_argument, &show_version, 1},
  78.   {0, 0, 0, 0},
  79. };
  80.  
  81. int
  82. main (int argc, char *const *argv)
  83. {
  84.   int option_char;        /* option character */
  85.   int counter;            /* general purpose counter */
  86.  
  87.   /* Decode command options.  */
  88.  
  89.   program_name = argv[0];
  90.   setlocale (LC_ALL, "");
  91.  
  92.   while (option_char = getopt_long (argc, argv, "l:", long_options, NULL),
  93.      option_char != EOF)
  94.     switch (option_char)
  95.       {
  96.       default:
  97.     usage (EXIT_FAILURE);
  98.  
  99.       case '\0':
  100.     break;
  101.  
  102.       case 'l':
  103.     file_length = atoi (optarg);
  104.     break;
  105.       }
  106.  
  107.   /* Process trivial options.  */
  108.  
  109.   if (show_version)
  110.     {
  111.       printf ("GNU %s %s\n", PACKAGE, VERSION);
  112.       exit (EXIT_SUCCESS);
  113.     }
  114.  
  115.   if (show_help)
  116.     usage (EXIT_SUCCESS);
  117.  
  118.   if (optind < argc)
  119.     usage (EXIT_FAILURE);
  120.  
  121.   /* Generate file.  */
  122.  
  123.   for (counter = 0; counter < file_length; counter++)
  124.     putchar (counter & 255);
  125.  
  126.   exit (0);
  127. }
  128.