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