From c907294abea66bfa9739fcd6a08415f4ea1142f8 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 22 Dec 2007 16:22:30 -0500 Subject: [PATCH] Add a name field to support LBX1 fallback. LBX1 fallback requires files to be named like "archive.lbx.XXX", so we need to keep track of "archive.lbx". --- src/lbx.c | 14 ++++++++++---- src/lbx.h | 2 +- src/lbxtool.c | 20 ++++++++++++++++---- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/lbx.c b/src/lbx.c index 7376730..6e7b9ee 100644 --- a/src/lbx.c +++ b/src/lbx.c @@ -12,14 +12,16 @@ 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; } diff --git a/src/lbx.h b/src/lbx.h index 563ef4e..1911cd7 100644 --- 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 *); diff --git a/src/lbxtool.c b/src/lbxtool.c index a8b899a..96c8ddd 100644 --- a/src/lbxtool.c +++ b/src/lbxtool.c @@ -1,6 +1,7 @@ #define _GNU_SOURCE #include #include +#include #include #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; } -- 2.43.2