]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbxtool.c
liblbx: Correct EOF handling in lbx_file_read.
[liblbx.git] / src / lbxtool.c
index 687e419bdf297e4f51b333aa98ac5bc194a0bb0f..3c4e436450caf0db09f6fe9f79cbbd653dd0add2 100644 (file)
@@ -1,16 +1,43 @@
-#define _GNU_SOURCE
+/*
+ *  2ooM: The Master of Orion II Reverse Engineering Project
+ *  Simple command-line tool to extract LBX archive files.
+ *  Copyright © 2006-2011, 2013 Nick Bowler
+ *
+ *  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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+#include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <getopt.h>
 #include <fnmatch.h>
 
+#include "tools.h"
 #include "lbx.h"
+#include "error.h"
 
-static const char *progname;
-#define errmsg(fmt, ...) (\
-       fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
-)
+static void printusage(void)
+{
+       puts("usage: lbxtool [-l|-x] [-v] [-f path] [file ...]");
+}
+
+static void printhelp(void)
+{
+       printusage();
+       puts("For now, see the man page for detailed help.");
+}
 
 enum {
        MODE_NONE,
@@ -20,7 +47,7 @@ enum {
 
 int filematch(char **argv, const char *name)
 {
-       int rc, i;
+       int i;
 
        for (i = 0; argv[i]; i++) {
                switch(fnmatch(argv[i], name, 0)) {
@@ -29,7 +56,7 @@ int filematch(char **argv, const char *name)
                case FNM_NOMATCH:
                        break;
                default:
-                       errmsg("error matching glob: %s.\n", argv[i]);
+                       tool_err(-1, "error matching glob: %s", argv[i]);
                        return 1;
                }
        }
@@ -37,31 +64,23 @@ int filematch(char **argv, const char *name)
        return i ? -1: 0;
 }
 
-int list(FILE *f, const char *name, int verbose, char **argv) {
-       LBX *lbx;
-       size_t nfiles;
+int list(struct lbx *lbx, int verbose, char **argv)
+{
        unsigned int i;
 
-       lbx = lbx_fopen(f, name);
-       if (!lbx) {
-               errmsg("failed to open archive: %s.\n", lbx_strerror());
-               return EXIT_FAILURE;
-       }
-
-       nfiles = lbx_numfiles(lbx);
        if (verbose) {
-               printf("Files in archive: %zu\n", nfiles);
+               printf("Files in archive: %u\n", lbx->nfiles);
        }
 
-       for (i = 0; i < nfiles; i++) {
+       for (i = 0; i < lbx->nfiles; i++) {
                struct lbx_statbuf stat;
 
-               lbx_stat(lbx, i, &stat);
+               lbx_file_stat(lbx, i, &stat);
 
                switch (filematch(argv, stat.name)) {
                case -1: continue;
                case  0: break;
-               default: goto err;
+               default: return EXIT_FAILURE;
                }
 
                printf("%s", stat.name);
@@ -72,76 +91,93 @@ int list(FILE *f, const char *name, int verbose, char **argv) {
                putchar('\n');
        }
 
-       lbx_close(lbx);
        return EXIT_SUCCESS;
-err:
-       lbx_close(lbx);
-       return EXIT_FAILURE;
 }
 
-int extract(FILE *f, const char *name, int verbose, char **argv) {
-       LBX *lbx;
-       size_t nfiles;
-       unsigned int i;
+int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
+{
+       int ret = -1;
+       size_t rc;
+       FILE *of;
+
+       of = fopen(stat->name, "wb");
+       if (!of) {
+               tool_err(0, "%s: fopen", stat->name);
+               return -1;
+       }
 
-       lbx = lbx_fopen(f, name);
-       if (!lbx) {
-               errmsg("failed to open archive: %s.\n", lbx_strerror());
-               return EXIT_FAILURE;
+       while (1) {
+               unsigned char buf[1024];
+
+               rc = lbx_file_read(f, buf, sizeof buf);
+               if (rc > 0) {
+                       /* Write out any data we got. */
+                       if (fwrite(buf, rc, 1, of) != 1) {
+                               tool_err(0, "%s: fwrite", stat->name);
+                               break;
+                       }
+               }
+
+               /* Now test for read errors */
+               if (rc < sizeof buf) {
+                       if (!lbx_file_eof(f))
+                               tool_err(-1, "error reading archive: %s", lbx_errmsg());
+                       else
+                               ret = 0;
+                       break;
+               }
+       }
+
+       if (fclose(of) == EOF) {
+               tool_err(0, "%s: fclose", stat->name);
+               return -1;
        }
 
-       nfiles = lbx_numfiles(lbx);
+       return ret;
+}
+
+int extract(struct lbx *lbx, int verbose, char **argv)
+{
+       unsigned int i;
+
        if (verbose) {
-               printf("Files in archive: %zu\n", nfiles);
+               printf("Files in archive: %u\n", lbx->nfiles);
        }
 
-       for (i = 0; i < nfiles; i++) {
+       for (i = 0; i < lbx->nfiles; i++) {
                struct lbx_statbuf stat;
-               size_t rc;
-               FILE *of;
-               int j;
+               LBXfile *file;
 
-               lbx_stat(lbx, i, &stat);
+               lbx_file_stat(lbx, i, &stat);
 
                switch (filematch(argv, stat.name)) {
                case -1: continue;
                case  0: break;
-               default: goto err;
+               default: return EXIT_FAILURE;
                }
 
-               of = fopen(stat.name, "wbx");
-               if (!of) {
-                       errmsg("failed to create output file %s: %m.\n",
-                                                            stat.name);
-                       goto err;
+               file = lbx_file_open(lbx, i);
+               if (!file) {
+                       tool_err(-1, "%s: %s", stat.name, lbx_errmsg());
+                       continue;
                }
 
-               if (verbose) printf("extracting %s...\n", stat.name);
-               rc = lbx_extract(lbx, i, of);
-               if (rc < stat.size) {
-                       if (rc == 0) remove(stat.name);
-                       errmsg("error extracting %s: %s.\n",
-                              stat.name, lbx_strerror());
-                       goto err;
-               }
-               if (verbose) printf("wrote %zu bytes.\n", rc);
+               if (extract_file(file, &stat) == -1)
+                       return EXIT_FAILURE;
+               lbx_file_close(file);
        }
 
-       lbx_close(lbx);
        return EXIT_SUCCESS;
-err:
-       lbx_close(lbx);
-       return EXIT_FAILURE;
 }
 
 int main(int argc, char **argv)
 {
-       int mode = MODE_NONE, verbose = 0;
-       const char *name = "stdin";
-       FILE *f  = stdin;
-       int opt;
+       int mode = MODE_NONE, verbose = 0, opt, rc = EXIT_FAILURE;
+       struct lbx_pipe_state stdin_handle = { .f = stdin };
+       const char *file = NULL;
+       struct lbx *lbx;
 
-       static const char         *sopts   = "lxf:i:v";
+       static const char         *sopts   = "lxf:i:vV";
        static const struct option lopts[] = {
                { "list",    0, NULL, 'l' },
                { "extract", 0, NULL, 'x' },
@@ -151,10 +187,14 @@ int main(int argc, char **argv)
 
                { "verbose", 0, NULL, 'v' },
 
+               { "version", 0, NULL, 'V' },
+               { "usage",   0, NULL, 'U' },
+               { "help",    0, NULL, 'H' },
+
                { 0 }
        };
 
-       progname = "lbxtool"; /* argv[0]; */
+       tool_init("lbxtool", argc, argv);
        while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
                switch(opt) {
                case 'l':
@@ -164,17 +204,7 @@ int main(int argc, char **argv)
                        mode = MODE_EXTRACT;
                        break;
                case 'f':
-                       if (strcmp(optarg, "-") == 0)
-                               break;
-
-                       name = strrchr(optarg, '/');
-                       name = name ? name+1 : optarg;
-
-                       f = fopen(optarg, "rb");
-                       if (!f) {
-                               errmsg("failed to open file %s: %m\n", optarg);
-                               return EXIT_FAILURE;
-                       }
+                       file = optarg;
                        break;
                case 'i':
                        /* FIXME: Add index file support. */
@@ -182,18 +212,41 @@ int main(int argc, char **argv)
                case 'v':
                        verbose = 1;
                        break;
+               case 'V':
+                       tool_version();
+                       return EXIT_SUCCESS;
+               case 'U':
+                       printusage();
+                       return EXIT_SUCCESS;
+               case 'H':
+                       printhelp();
+                       return EXIT_SUCCESS;
                default:
                        return EXIT_FAILURE;
                }
        }
 
+       if (file)
+               lbx = lbx_fopen(file);
+       else
+               lbx = lbx_open(&stdin_handle, &lbx_pipe_fops, NULL, "stdin");
+
+       if (!lbx) {
+               tool_err(-1, "%s: %s", file ? file : "stdin", lbx_errmsg());
+               return EXIT_FAILURE;
+       }
+
        switch (mode) {
        case MODE_LIST:
-               return list(f, name, verbose, &argv[optind]);
+               rc = list(lbx, verbose, &argv[optind]);
+               break;
        case MODE_EXTRACT:
-               return extract(f, name, verbose, &argv[optind]);
+               rc = extract(lbx, verbose, &argv[optind]);
+               break;
+       default:
+               tool_err(-1, "no mode specified");
        }
 
-       fprintf(stderr, "%s: you must specify a mode.\n", progname);
-       return EXIT_FAILURE;
+       lbx_close(lbx);
+       return rc;
 }