2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Simple command-line tool to extract LBX archive files.
4 * Copyright (C) 2006-2010 Nick Bowler
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 static void printusage(void)
33 puts("usage: lbxtool [-l|-x] [-v] [-f path] [file ...]");
36 static void printhelp(void)
39 puts("For now, see the man page for detailed help.");
42 static const char *progname;
43 #define errmsg(fmt, ...) (\
44 fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
53 int filematch(char **argv, const char *name)
57 for (i = 0; argv[i]; i++) {
58 switch(fnmatch(argv[i], name, 0)) {
64 errmsg("error matching glob: %s.\n", argv[i]);
72 int list(struct lbx *lbx, int verbose, char **argv)
77 printf("Files in archive: %u\n", lbx->nfiles);
80 for (i = 0; i < lbx->nfiles; i++) {
81 struct lbx_statbuf stat;
83 lbx_file_stat(lbx, i, &stat);
85 switch (filematch(argv, stat.name)) {
88 default: return EXIT_FAILURE;
91 printf("%s", stat.name);
93 printf(" size=%zu bytes", stat.size);
102 int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
108 of = fopen(stat->name, "wb");
110 errmsg("%s: fopen: %s\n",
111 stat->name, strerror(errno));
116 unsigned char buf[1024];
118 rc = lbx_file_read(f, buf, sizeof buf);
125 if (fwrite(buf, rc, 1, of) != 1) {
126 errmsg("%s: fwrite: %s\n", stat->name, strerror(errno));
130 if (rc < sizeof buf) {
137 if (fclose(of) == EOF) {
138 errmsg("%s: fclose: %s\n", stat->name, strerror(errno));
145 int extract(struct lbx *lbx, int verbose, char **argv)
150 printf("Files in archive: %u\n", lbx->nfiles);
153 for (i = 0; i < lbx->nfiles; i++) {
154 struct lbx_statbuf stat;
157 lbx_file_stat(lbx, i, &stat);
159 switch (filematch(argv, stat.name)) {
162 default: return EXIT_FAILURE;
165 file = lbx_file_open(lbx, i);
167 errmsg("%s: %s.\n", stat.name, lbx_errmsg());
171 if (extract_file(file, &stat) == -1)
173 lbx_file_close(file);
179 int main(int argc, char **argv)
181 int mode = MODE_NONE, verbose = 0, opt, rc = EXIT_FAILURE;
182 struct lbx_pipe_state stdin_handle = { .f = stdin };
183 const char *file = NULL;
186 static const char *sopts = "lxf:i:vV";
187 static const struct option lopts[] = {
188 { "list", 0, NULL, 'l' },
189 { "extract", 0, NULL, 'x' },
191 { "file", 1, NULL, 'f' },
192 { "index", 1, NULL, 'i' },
194 { "verbose", 0, NULL, 'v' },
196 { "version", 0, NULL, 'V' },
197 { "usage", 0, NULL, 'U' },
198 { "help", 0, NULL, 'H' },
203 progname = "lbxtool"; /* argv[0]; */
204 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
216 /* FIXME: Add index file support. */
222 puts(VERSION_BOILERPLATE("lbxtool"));
236 lbx = lbx_fopen(file);
238 lbx = lbx_open(&stdin_handle, &lbx_pipe_fops, NULL, "stdin");
241 errmsg("%s: %s.\n", file ? file : "stdin", lbx_errmsg());
247 rc = list(lbx, verbose, &argv[optind]);
250 rc = extract(lbx, verbose, &argv[optind]);
253 fprintf(stderr, "%s: you must specify a mode.\n", progname);