]> Dogcows Code - chaz/tar/blob - tests/genfile.c
Merge recent gnulib changes, and remove some lint.
[chaz/tar] / tests / genfile.c
1 /* Generate a file containing some preset patterns.
2
3 Copyright (C) 1995, 1996, 1997, 2001, 2003, 2004 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 static void usage (int) __attribute__ ((noreturn));
58 static void
59 usage (int status)
60 {
61 if (status != EXIT_SUCCESS)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 program_name);
64 else
65 {
66 printf (_("Generate data files for GNU tar test suite.\n"));
67 printf (_("\
68 \n\
69 Usage: %s [OPTION]...\n"), program_name);
70 fputs (_("\
71 If a long option shows an argument as mandatory, then it is mandatory\n\
72 for the equivalent short option also.\n\
73 \n\
74 -l, --file-length=LENGTH LENGTH of generated file\n\
75 -p, --pattern=PATTERN PATTERN is `default' or `zeros'\n\
76 --help display this help and exit\n\
77 --version output version information and exit\n"),
78 stdout);
79 }
80 exit (status);
81 }
82
83 /* Main program. Decode ARGC arguments passed through the ARGV array
84 of strings, then launch execution. */
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 static char const * const pattern_args[] = { "default", "zeros", 0 };
97 static enum pattern const pattern_types[] = {DEFAULT_PATTERN, ZEROS_PATTERN};
98
99 int
100 main (int argc, char *const *argv)
101 {
102 int option_char; /* option character */
103 int counter; /* general purpose counter */
104
105 /* Decode command options. */
106
107 program_name = argv[0];
108 setlocale (LC_ALL, "");
109
110 while (option_char = getopt_long (argc, argv, "l:p:", long_options, NULL),
111 option_char != EOF)
112 switch (option_char)
113 {
114 default:
115 usage (EXIT_FAILURE);
116
117 case '\0':
118 break;
119
120 case 'l':
121 file_length = atoi (optarg);
122 break;
123
124 case 'p':
125 pattern = XARGMATCH ("--pattern", optarg,
126 pattern_args, pattern_types);
127 break;
128 }
129
130 /* Process trivial options. */
131
132 if (show_version)
133 {
134 printf ("genfile (GNU %s) %s\n", PACKAGE, VERSION);
135 printf (_("Copyright (C) %d Free Software Foundation, Inc.\n"), 2003);
136 puts (_("\
137 This program comes with NO WARRANTY, to the extent permitted by law.\n\
138 You may redistribute it under the terms of the GNU General Public License;\n\
139 see the file named COPYING for details."));
140
141 /* Note to translator: Please translate "F. Pinard" to "François
142 Pinard" if "ç" (c-with-cedilla) is available in the
143 translation's character set and encoding. */
144 puts (_("Written by F. Pinard."));
145
146 exit (EXIT_SUCCESS);
147 }
148
149 if (show_help)
150 usage (EXIT_SUCCESS);
151
152 if (optind < argc)
153 usage (EXIT_FAILURE);
154
155 /* Generate file. */
156
157 switch (pattern)
158 {
159 case DEFAULT_PATTERN:
160 for (counter = 0; counter < file_length; counter++)
161 putchar (counter & 255);
162 break;
163
164 case ZEROS_PATTERN:
165 for (counter = 0; counter < file_length; counter++)
166 putchar (0);
167 break;
168 }
169
170 exit (0);
171 }
This page took 0.038547 seconds and 4 git commands to generate.