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;
}
*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:
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;
}
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <getopt.h>
#include "lbx.h"
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;
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' },
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;
switch (mode) {
case MODE_LIST:
- return list(f, verbose);
+ return list(f, name, verbose);
case MODE_EXTRACT:
return EXIT_SUCCESS;
}