2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Simple command-line tool to extract LBX archive files.
4 * Copyright © 2006-2011, 2013 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.");
48 int filematch(char **argv, const char *name)
52 for (i = 0; argv[i]; i++) {
53 switch(fnmatch(argv[i], name, 0)) {
59 tool_err(-1, "error matching glob: %s", argv[i]);
67 int list(struct lbx *lbx, int verbose, char **argv)
72 printf("Files in archive: %u\n", lbx->nfiles);
75 for (i = 0; i < lbx->nfiles; i++) {
76 struct lbx_statbuf stat;
78 lbx_file_stat(lbx, i, &stat);
80 switch (filematch(argv, stat.name)) {
83 default: return EXIT_FAILURE;
86 printf("%s", stat.name);
88 printf(" size=%zu bytes", stat.size);
97 int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
103 of = fopen(stat->name, "wb");
105 tool_err(0, "%s: fopen", stat->name);
110 unsigned char buf[1024];
112 rc = lbx_file_read(f, buf, sizeof buf);
119 if (fwrite(buf, rc, 1, of) != 1) {
120 tool_err(0, "%s: fwrite", stat->name);
124 if (rc < sizeof buf) {
131 if (fclose(of) == EOF) {
132 tool_err(0, "%s: fclose", stat->name);
139 int extract(struct lbx *lbx, int verbose, char **argv)
144 printf("Files in archive: %u\n", lbx->nfiles);
147 for (i = 0; i < lbx->nfiles; i++) {
148 struct lbx_statbuf stat;
151 lbx_file_stat(lbx, i, &stat);
153 switch (filematch(argv, stat.name)) {
156 default: return EXIT_FAILURE;
159 file = lbx_file_open(lbx, i);
161 tool_err(-1, "%s: %s", stat.name, lbx_errmsg());
165 if (extract_file(file, &stat) == -1)
167 lbx_file_close(file);
173 int main(int argc, char **argv)
175 int mode = MODE_NONE, verbose = 0, opt, rc = EXIT_FAILURE;
176 struct lbx_pipe_state stdin_handle = { .f = stdin };
177 const char *file = NULL;
180 static const char *sopts = "lxf:i:vV";
181 static const struct option lopts[] = {
182 { "list", 0, NULL, 'l' },
183 { "extract", 0, NULL, 'x' },
185 { "file", 1, NULL, 'f' },
186 { "index", 1, NULL, 'i' },
188 { "verbose", 0, NULL, 'v' },
190 { "version", 0, NULL, 'V' },
191 { "usage", 0, NULL, 'U' },
192 { "help", 0, NULL, 'H' },
197 tool_init("lbxtool", argc, argv);
198 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
210 /* FIXME: Add index file support. */
230 lbx = lbx_fopen(file);
232 lbx = lbx_open(&stdin_handle, &lbx_pipe_fops, NULL, "stdin");
235 tool_err(-1, "%s: %s", file ? file : "stdin", lbx_errmsg());
241 rc = list(lbx, verbose, &argv[optind]);
244 rc = extract(lbx, verbose, &argv[optind]);
247 tool_err(-1, "no mode specified");