]> git.draconx.ca Git - liblbx.git/commitdiff
Add a name field to support LBX1 fallback.
authorNick Bowler <draconx@gmail.com>
Sat, 22 Dec 2007 21:22:30 +0000 (16:22 -0500)
committerNick Bowler <draconx@gmail.com>
Sat, 22 Dec 2007 21:49:19 +0000 (16:49 -0500)
LBX1 fallback requires files to be named like "archive.lbx.XXX", so we need
to keep track of "archive.lbx".

src/lbx.c
src/lbx.h
src/lbxtool.c

index 73767305c4e3fb54690a3d09166612a5300ef4c8..6e7b9eef790af843107e4a316163895d404e9df9 100644 (file)
--- a/src/lbx.c
+++ b/src/lbx.c
 int lbx_errno = 0;
 
 struct lbx_state {
+       const char *name;
+
        FILE *f;
-       long offset;
+       long foff;
 
        uint16_t nfiles;
        uint32_t offsets[];
 };
 
-struct lbx_state *lbx_fopen(FILE *f)
+struct lbx_state *lbx_fopen(FILE *f, const char *name)
 {
        struct lbx_state *new = NULL;
        uint16_t nfiles, version;
@@ -45,12 +47,15 @@ struct lbx_state *lbx_fopen(FILE *f)
        }
        
        *new = (struct lbx_state){
+               .name   = name,
                .nfiles = nfiles,
-               .f = f,
+               .f      = f,
+               .foff   = sizeof nfiles + sizeof magic + sizeof version,
        };
        
        if (fread(new->offsets, sizeof *new->offsets, nfiles+1, f) != nfiles+1)
                goto readerr;
+       new->foff += sizeof *new->offsets * (nfiles+1);
 
        return new;
 readerr:
@@ -70,7 +75,8 @@ struct lbx_state *lbx_open(const char *path)
        FILE *f;
        
        if ((f = fopen(path, "rb"))) {
-               new = lbx_fopen(f);
+               const char *name = strrchr(path, '/');
+               new = lbx_fopen(f, name ? name+1 : path);
        } else {
                lbx_errno = -errno;
        }
index 563ef4eb81e55b30195a05559cdaaad4c5d60773..1911cd759012f3ebeb835510a597d75fd16a2051 100644 (file)
--- a/src/lbx.h
+++ b/src/lbx.h
@@ -15,7 +15,7 @@ extern int lbx_errno;
 typedef struct lbx_state LBX;
 
 /* File operations */
-LBX   *lbx_fopen(FILE *);
+LBX   *lbx_fopen(FILE *, const char *);
 LBX   *lbx_open(const char *);
 void   lbx_close(LBX *);
 size_t lbx_numfiles(LBX *);
index a8b899ac80a10d4d0c7317b6d122e371b8d62d12..96c8ddd06e8ef87c7a016f2923e4cfac9967fa98 100644 (file)
@@ -1,6 +1,7 @@
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <getopt.h>
 
 #include "lbx.h"
@@ -16,8 +17,8 @@ enum {
        MODE_EXTRACT,
 };
 
-int list(FILE *f, int verbose) {
-       LBX *lbx = lbx_fopen(f);
+int list(FILE *f, const char *name, int verbose) {
+       LBX *lbx = lbx_fopen(f, name);
        if (!lbx) {
                errmsg("failed to open archive: %s.\n", lbx_strerror());
                return EXIT_FAILURE;
@@ -33,15 +34,17 @@ int list(FILE *f, int verbose) {
 int main(int argc, char **argv)
 {
        int mode = MODE_NONE, verbose = 0;
+       const char *name = "stdin";
        FILE *f  = stdin;
        int opt;
 
-       static const char         *sopts   = "lxf:v";
+       static const char         *sopts   = "lxf:i:v";
        static const struct option lopts[] = {
                { "list",    0, NULL, 'l' },
                { "extract", 0, NULL, 'x' },
 
                { "file",    1, NULL, 'f' },
+               { "index",   1, NULL, 'i' },
 
                { "verbose", 0, NULL, 'v' },
 
@@ -58,12 +61,21 @@ 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;
                        }
                        break;
+               case 'i':
+                       /* FIXME: Add index file support. */
+                       break;
                case 'v':
                        verbose = 1;
                        break;
@@ -74,7 +86,7 @@ int main(int argc, char **argv)
 
        switch (mode) {
        case MODE_LIST:
-               return list(f, verbose);
+               return list(f, name, verbose);
        case MODE_EXTRACT:
                return EXIT_SUCCESS;
        }