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-2014, 2021 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/>.
35 static const char sopts[] = SOPT_STRING;
36 static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
38 static void print_usage(FILE *f)
40 const char *progname = tool_invocation();
42 fprintf(f, "Usage: %s [options] [-l|-x] [file ...]\n", progname);
44 fprintf(f, "Try %s --help for more information.\n", progname);
47 static void print_help(void)
49 const struct option *opt;
53 puts("This is \"lbxtool\": a command-line tool for manipulating the LBX archives\n"
58 for (opt = lopts; opt->name; opt++) {
59 struct lopt_help help;
62 if (!lopt_get_help(opt, &help))
65 help_print_option(opt, help.arg, help.desc, 20);
69 puts("For more information, see the lbxtool(1) man page.");
72 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
81 int filematch(char **argv, const char *name)
85 for (i = 0; argv[i]; i++) {
86 switch(fnmatch(argv[i], name, 0)) {
92 tool_err(-1, "error matching glob: %s", argv[i]);
100 int list(struct lbx *lbx, int verbose, char **argv)
105 printf("Files in archive: %u\n", lbx->nfiles);
108 for (i = 0; i < lbx->nfiles; i++) {
109 struct lbx_statbuf stat;
111 lbx_file_stat(lbx, i, &stat);
113 switch (filematch(argv, stat.name)) {
116 default: return EXIT_FAILURE;
119 printf("%s", stat.name);
121 printf(" size=%zu bytes", stat.size);
130 int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
136 of = fopen(stat->name, "wb");
138 tool_err(0, "%s: fopen", stat->name);
143 unsigned char buf[1024];
145 rc = lbx_file_read(f, buf, sizeof buf);
147 /* Write out any data we got. */
148 if (fwrite(buf, rc, 1, of) != 1) {
149 tool_err(0, "%s: fwrite", stat->name);
154 /* Now test for read errors */
155 if (rc < sizeof buf) {
156 if (!lbx_file_eof(f))
157 tool_err(-1, "error reading archive: %s", lbx_errmsg());
164 if (fclose(of) == EOF) {
165 tool_err(0, "%s: fclose", stat->name);
172 int extract(struct lbx *lbx, int verbose, char **argv)
177 printf("Files in archive: %u\n", lbx->nfiles);
180 for (i = 0; i < lbx->nfiles; i++) {
181 struct lbx_statbuf stat;
184 lbx_file_stat(lbx, i, &stat);
186 switch (filematch(argv, stat.name)) {
189 default: return EXIT_FAILURE;
192 file = lbx_file_open(lbx, i);
194 tool_err(-1, "%s: %s", stat.name, lbx_errmsg());
198 if (extract_file(file, &stat) == -1)
200 lbx_file_close(file);
206 int main(int argc, char **argv)
208 int mode = MODE_NONE, verbose = 0, opt, rc = EXIT_FAILURE;
209 struct lbx_pipe_state stdin_handle = { .f = stdin };
210 const char *file = NULL;
213 tool_init("lbxtool", argc, argv);
214 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
240 lbx = lbx_fopen(file);
242 lbx = lbx_open(&stdin_handle, &lbx_pipe_fops, NULL, "stdin");
245 tool_err(-1, "%s: %s", file ? file : "stdin", lbx_errmsg());
251 rc = list(lbx, verbose, &argv[optind]);
254 rc = extract(lbx, verbose, &argv[optind]);
257 tool_err(-1, "no mode specified");