quakeforge/tools/pak/pak.c

244 lines
5.8 KiB
C
Raw Normal View History

/*
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
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <getopt.h>
#include <errno.h>
#include <string.h>
2002-05-06 16:36:07 +00:00
#include <stdlib.h>
#include <QF/qtypes.h>
#include <QF/pakfile.h>
#include "pak.h"
const char *this_program;
options_t options;
static const struct option long_options[] = {
{"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'},
{NULL, 0, NULL, 0},
};
static void
usage (int status)
{
printf ("%s - QuakeForge Packfile tool\n", this_program);
printf ("Usage: %s <command> [options] ARCHIVE [FILE...]\n",
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");
printf ("Options:\n"
" -f, --file ARCHIVE Use ARCHIVE for archive filename\n"
" -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)
{
int c;
options.mode = mo_none;
options.pad = false;
options.packfile = NULL;
2002-08-13 19:32:06 +00:00
options.verbosity = 0;
while ((c = getopt_long (argc, argv, "c" // create archive
"t" // test archive
"x" // extract archive contents
"h" // show help
"V" // show version
"f:" // filename
"p" // pad
"q" // quiet
"v" // verbose
, long_options, (int *) 0)) != EOF) {
switch (c) {
case 'h': // help
usage (0);
break;
case 'V': // version
printf ("pak version %s\n", PACKAGE_VERSION);
exit (0);
break;
case 'c': // create
options.mode = mo_create;
break;
case 't': // test
options.mode = mo_test;
2002-03-18 18:03:26 +00:00
break;
case 'x': // extract
options.mode = mo_extract;
2002-03-18 21:37:39 +00:00
break;
case 'f': // set filename
options.packfile = strdup (optarg);
break;
case 'q': // lower verbosity
options.verbosity--;
break;
case 'p': // pad
options.pad = true;
2002-03-18 21:37:39 +00:00
break;
case 'v': // increase verbosity
options.verbosity++;
break;
default:
usage (1);
}
}
if ((!options.packfile) && argv[optind] && *(argv[optind]))
options.packfile = strdup (argv[optind++]);
return optind;
}
int
main (int argc, char **argv)
{
pack_t *pack;
int i, j, rehash = 0;
dpackfile_t *pf;
this_program = argv[0];
decode_args (argc, argv);
if (!options.packfile) {
fprintf (stderr, "%s: no archive file specified.\n",
this_program);
usage (1);
}
switch (options.mode) {
2002-03-18 21:37:39 +00:00
case mo_extract:
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++) {
pf = &pack->files[i];
for (j = 0; j < PAK_PATH_LENGTH; j++) {
if (!pf->name[j])
break;
if (pf->name[j] == '\\') {
pf->name[j] = '/';
rehash = 1;
}
}
if (optind == argc) {
if (options.verbosity > 0)
printf ("%s\n", pf->name);
pack_extract (pack, pf);
}
}
if (optind < argc) {
if (rehash)
pack_rehash (pack);
while (optind < argc) {
pf = pack_find_file (pack, argv[optind]);
if (!pf) {
2003-09-12 17:14:39 +00:00
fprintf (stderr, "could not find %s\n", argv[optind]);
2006-08-20 06:23:24 +00:00
optind++;
continue;
}
if (options.verbosity > 0)
printf ("%s\n", pf->name);
pack_extract (pack, pf);
optind++;
}
2002-03-18 21:37:39 +00:00
}
pack_close (pack);
break;
case mo_test:
if (!(pack = pack_open (options.packfile))) {
fprintf (stderr, "%s: error opening %s: %s\n", this_program,
options.packfile,
strerror (errno));
return 1;
}
2002-03-18 21:37:39 +00:00
for (i = 0; i < pack->numfiles; i++) {
if (options.verbosity >= 1)
printf ("%6d ", pack->files[i].filelen);
if (options.verbosity >= 0)
2002-03-18 21:37:39 +00:00
printf ("%s\n", pack->files[i].name);
}
pack_close (pack);
break;
2002-03-18 18:03:26 +00:00
case mo_create:
pack = pack_create (options.packfile);
2002-03-18 18:03:26 +00:00
if (!pack) {
fprintf (stderr, "%s: error creating %s: %s\n", this_program,
options.packfile,
strerror (errno));
2002-03-18 18:03:26 +00:00
return 1;
}
pack->pad = options.pad;
2002-03-18 21:37:39 +00:00
while (optind < argc) {
if (options.verbosity > 0)
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;
default:
fprintf (stderr, "%s: No command given.\n",
this_program);
usage (1);
}
return 0;
}