#include "system.h"
#include <argmatch.h>
#include <getopt.h>
#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
# define EXIT_FAILURE 1
#endif
enum pattern
{
DEFAULT_PATTERN,
ZEROS_PATTERN
};
const char *program_name;
static int show_help = 0;
static int show_version = 0;
static int file_length = 0;
static enum pattern pattern = DEFAULT_PATTERN;
static void usage (int) __attribute__ ((noreturn));
static void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("Generate data files for GNU tar test suite.\n"));
printf (_("\
\n\
Usage: %s [OPTION]...\n"), program_name);
fputs (_("\
If a long option shows an argument as mandatory, then it is mandatory\n\
for the equivalent short option also.\n\
\n\
-l, --file-length=LENGTH LENGTH of generated file\n\
-p, --pattern=PATTERN PATTERN is `default' or `zeros'\n\
--help display this help and exit\n\
--version output version information and exit\n"),
stdout);
}
exit (status);
}
static const struct option long_options[] =
{
{"help", no_argument, &show_help, 1},
{"length", required_argument, NULL, 'l'},
{"pattern", required_argument, NULL, 'p'},
{"version", no_argument, &show_version, 1},
{0, 0, 0, 0},
};
static char const * const pattern_args[] = { "default", "zeros", 0 };
static enum pattern const pattern_types[] = {DEFAULT_PATTERN, ZEROS_PATTERN};
int
main (int argc, char *const *argv)
{
int option_char;
int counter;
program_name = argv[0];
setlocale (LC_ALL, "");
while (option_char = getopt_long (argc, argv, "l:p:", long_options, NULL),
option_char != EOF)
switch (option_char)
{
default:
usage (EXIT_FAILURE);
case '\0':
break;
case 'l':
file_length = atoi (optarg);
break;
case 'p':
pattern = XARGMATCH ("--pattern", optarg,
pattern_args, pattern_types);
break;
}
if (show_version)
{
printf ("genfile (GNU %s) %s\n", PACKAGE, VERSION);
printf (_("Copyright (C) %d Free Software Foundation, Inc.\n"), 2003);
puts (_("\
This program comes with NO WARRANTY, to the extent permitted by law.\n\
You may redistribute it under the terms of the GNU General Public License;\n\
see the file named COPYING for details."));
puts (_("Written by F. Pinard."));
exit (EXIT_SUCCESS);
}
if (show_help)
usage (EXIT_SUCCESS);
if (optind < argc)
usage (EXIT_FAILURE);
switch (pattern)
{
case DEFAULT_PATTERN:
for (counter = 0; counter < file_length; counter++)
putchar (counter & 255);
break;
case ZEROS_PATTERN:
for (counter = 0; counter < file_length; counter++)
putchar (0);
break;
}
exit (0);
}