2002-05-03 10:38:04 +00:00
|
|
|
/*
|
|
|
|
pak.c
|
|
|
|
|
|
|
|
Pakfile tool
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
|
|
|
Copyright (C) 2002 Jeff Teunissen <deek@quakeforge.net>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public
|
|
|
|
License along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-05-25 02:47:53 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2002-03-15 18:52:12 +00:00
|
|
|
#include <getopt.h>
|
2002-05-03 10:38:04 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2002-05-06 16:36:07 +00:00
|
|
|
#include <stdlib.h>
|
2002-05-03 10:38:04 +00:00
|
|
|
|
|
|
|
#include <QF/qtypes.h>
|
2002-07-21 06:11:28 +00:00
|
|
|
#include <QF/pakfile.h>
|
2002-03-15 18:52:12 +00:00
|
|
|
|
2002-05-03 10:38:04 +00:00
|
|
|
#include "pak.h"
|
|
|
|
|
|
|
|
const char *this_program;
|
|
|
|
options_t options;
|
2002-03-12 23:45:36 +00:00
|
|
|
|
2002-03-15 18:52:12 +00:00
|
|
|
static const struct option long_options[] = {
|
2002-05-03 10:38:04 +00:00
|
|
|
{"create", no_argument, 0, 'c'},
|
|
|
|
{"test", no_argument, 0, 't'},
|
|
|
|
{"extract", no_argument, 0, 'x'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"version", no_argument, 0, 'V'},
|
|
|
|
{"pad", no_argument, 0, 'p'},
|
|
|
|
{"quiet", no_argument, 0, 'q'},
|
|
|
|
{"verbose", no_argument, 0, 'v'},
|
2002-03-15 18:52:12 +00:00
|
|
|
{NULL, 0, NULL, 0},
|
|
|
|
};
|
|
|
|
|
2002-05-03 10:38:04 +00:00
|
|
|
static void
|
|
|
|
usage (int status)
|
|
|
|
{
|
|
|
|
printf ("%s - QuakeForge Packfile tool\n", this_program);
|
2002-05-25 02:47:53 +00:00
|
|
|
printf ("Usage: %s <command> [options] ARCHIVE [FILE...]\n",
|
2002-05-03 10:38:04 +00:00
|
|
|
this_program);
|
|
|
|
|
|
|
|
printf ("Commands:\n"
|
|
|
|
" -c, --create Create archive\n"
|
|
|
|
" -t, --test Test archive\n"
|
|
|
|
" -x, --extract Extract archive contents\n"
|
|
|
|
" -h, --help Display this help and exit\n"
|
|
|
|
" -V, --version Output version information and exit\n\n");
|
2002-03-15 18:52:12 +00:00
|
|
|
|
2002-05-03 10:38:04 +00:00
|
|
|
printf ("Options:\n"
|
2002-05-25 02:47:53 +00:00
|
|
|
" -f, --file ARCHIVE Use ARCHIVE for archive filename\n"
|
2002-05-03 10:38:04 +00:00
|
|
|
" -p, --pad Pad file space to a 32-bit boundary\n"
|
|
|
|
" -q, --quiet Inhibit usual output\n"
|
|
|
|
" -v, --verbose Display more output than usual\n");
|
|
|
|
exit (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
decode_args (int argc, char **argv)
|
2002-03-12 23:45:36 +00:00
|
|
|
{
|
2002-05-03 10:38:04 +00:00
|
|
|
int c;
|
|
|
|
|
|
|
|
options.mode = mo_none;
|
|
|
|
options.pad = false;
|
|
|
|
options.packfile = NULL;
|
2002-08-13 19:32:06 +00:00
|
|
|
options.verbosity = 0;
|
2002-05-03 10:38:04 +00:00
|
|
|
|
|
|
|
while ((c = getopt_long (argc, argv, "c" // create archive
|
|
|
|
"t" // test archive
|
|
|
|
"x" // extract archive contents
|
|
|
|
"h" // show help
|
|
|
|
"V" // show version
|
2002-05-25 02:47:53 +00:00
|
|
|
"f:" // filename
|
2002-05-03 10:38:04 +00:00
|
|
|
"p" // pad
|
|
|
|
"q" // quiet
|
|
|
|
"v" // verbose
|
|
|
|
, long_options, (int *) 0)) != EOF) {
|
2002-03-15 18:52:12 +00:00
|
|
|
switch (c) {
|
2002-05-03 10:38:04 +00:00
|
|
|
case 'h': // help
|
|
|
|
usage (0);
|
|
|
|
break;
|
|
|
|
case 'V': // version
|
|
|
|
printf ("pak version %s\n", VERSION);
|
|
|
|
exit (0);
|
2002-03-15 18:52:12 +00:00
|
|
|
break;
|
2002-05-03 10:38:04 +00:00
|
|
|
case 'c': // create
|
|
|
|
options.mode = mo_create;
|
2002-03-15 18:52:12 +00:00
|
|
|
break;
|
2002-05-03 10:38:04 +00:00
|
|
|
case 't': // test
|
|
|
|
options.mode = mo_test;
|
2002-03-18 18:03:26 +00:00
|
|
|
break;
|
2002-05-03 10:38:04 +00:00
|
|
|
case 'x': // extract
|
|
|
|
options.mode = mo_extract;
|
2002-03-18 21:37:39 +00:00
|
|
|
break;
|
2002-05-25 02:47:53 +00:00
|
|
|
case 'f': // set filename
|
|
|
|
options.packfile = strdup (optarg);
|
|
|
|
break;
|
2002-05-03 10:38:04 +00:00
|
|
|
case 'q': // lower verbosity
|
|
|
|
options.verbosity--;
|
2002-03-15 18:52:12 +00:00
|
|
|
break;
|
2002-05-03 10:38:04 +00:00
|
|
|
case 'p': // pad
|
|
|
|
options.pad = true;
|
2002-03-18 21:37:39 +00:00
|
|
|
break;
|
2002-05-03 10:38:04 +00:00
|
|
|
case 'v': // increase verbosity
|
|
|
|
options.verbosity++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage (1);
|
2002-03-15 18:52:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-25 02:47:53 +00:00
|
|
|
if ((!options.packfile) && argv[optind] && *(argv[optind]))
|
2002-05-03 10:38:04 +00:00
|
|
|
options.packfile = strdup (argv[optind++]);
|
|
|
|
|
|
|
|
return optind;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
pack_t *pack;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
this_program = argv[0];
|
|
|
|
|
|
|
|
decode_args (argc, argv);
|
|
|
|
|
|
|
|
if (!options.packfile) {
|
2002-05-25 02:47:53 +00:00
|
|
|
fprintf (stderr, "%s: no archive file specified.\n",
|
2002-05-03 10:38:04 +00:00
|
|
|
this_program);
|
2002-05-25 02:47:53 +00:00
|
|
|
usage (1);
|
2002-03-15 18:52:12 +00:00
|
|
|
}
|
2002-03-12 23:45:36 +00:00
|
|
|
|
2002-05-03 10:38:04 +00:00
|
|
|
switch (options.mode) {
|
2002-03-18 21:37:39 +00:00
|
|
|
case mo_extract:
|
2002-05-03 10:38:04 +00:00
|
|
|
if (!(pack = pack_open (options.packfile))) {
|
|
|
|
fprintf (stderr, "%s: error opening %s: %s\n", this_program,
|
|
|
|
options.packfile,
|
|
|
|
strerror (errno));
|
2002-03-18 21:37:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < pack->numfiles; i++) {
|
2002-05-03 10:38:04 +00:00
|
|
|
if (options.verbosity > 0)
|
2002-03-18 22:16:14 +00:00
|
|
|
printf ("%s\n", pack->files[i].name);
|
2002-03-18 21:37:39 +00:00
|
|
|
pack_extract (pack, &pack->files[i]);
|
|
|
|
}
|
|
|
|
pack_close (pack);
|
|
|
|
break;
|
2002-03-15 18:52:12 +00:00
|
|
|
case mo_test:
|
2002-05-03 10:38:04 +00:00
|
|
|
if (!(pack = pack_open (options.packfile))) {
|
|
|
|
fprintf (stderr, "%s: error opening %s: %s\n", this_program,
|
|
|
|
options.packfile,
|
|
|
|
strerror (errno));
|
2002-03-15 18:52:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-03-18 21:37:39 +00:00
|
|
|
for (i = 0; i < pack->numfiles; i++) {
|
2002-08-14 22:38:24 +00:00
|
|
|
if (options.verbosity >= 1)
|
2002-05-03 10:38:04 +00:00
|
|
|
printf ("%6d ", pack->files[i].filelen);
|
2002-08-14 22:38:24 +00:00
|
|
|
if (options.verbosity >= 0)
|
2002-03-18 21:37:39 +00:00
|
|
|
printf ("%s\n", pack->files[i].name);
|
|
|
|
}
|
2002-03-15 18:52:12 +00:00
|
|
|
pack_close (pack);
|
|
|
|
break;
|
2002-03-18 18:03:26 +00:00
|
|
|
case mo_create:
|
2002-05-03 10:38:04 +00:00
|
|
|
pack = pack_create (options.packfile);
|
2002-03-18 18:03:26 +00:00
|
|
|
if (!pack) {
|
2002-05-03 10:38:04 +00:00
|
|
|
fprintf (stderr, "%s: error creating %s: %s\n", this_program,
|
|
|
|
options.packfile,
|
|
|
|
strerror (errno));
|
2002-03-18 18:03:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-05-03 10:38:04 +00:00
|
|
|
pack->pad = options.pad;
|
2002-03-18 21:37:39 +00:00
|
|
|
while (optind < argc) {
|
2002-05-03 10:38:04 +00:00
|
|
|
if (options.verbosity > 0)
|
2002-03-18 22:16:14 +00:00
|
|
|
printf ("%s\n", argv[optind]);
|
2002-03-18 18:03:26 +00:00
|
|
|
pack_add (pack, argv[optind++]);
|
2002-03-18 21:37:39 +00:00
|
|
|
}
|
2002-03-18 18:03:26 +00:00
|
|
|
pack_close (pack);
|
|
|
|
break;
|
2002-03-15 18:52:12 +00:00
|
|
|
default:
|
2002-05-25 02:47:53 +00:00
|
|
|
fprintf (stderr, "%s: No command given.\n",
|
2002-05-03 10:38:04 +00:00
|
|
|
this_program);
|
2002-05-25 02:47:53 +00:00
|
|
|
usage (1);
|
2002-03-15 18:52:12 +00:00
|
|
|
}
|
2002-03-12 23:45:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|