9 static const char *progname;
10 #define errmsg(fmt, ...) (\
11 fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
20 int list(FILE *f, const char *name, int verbose) {
25 lbx = lbx_fopen(f, name);
27 errmsg("failed to open archive: %s.\n", lbx_strerror());
31 nfiles = lbx_numfiles(lbx);
33 printf("Files in archive: %zu\n", nfiles);
36 for (i = 0; i < nfiles; i++) {
37 struct lbx_statbuf stat;
39 lbx_stat(lbx, i, &stat);
40 printf("%s", stat.name);
42 printf(" size=%zu bytes", stat.size);
52 int extract(FILE *f, const char *name, int verbose) {
57 lbx = lbx_fopen(f, name);
59 errmsg("failed to open archive: %s.\n", lbx_strerror());
63 nfiles = lbx_numfiles(lbx);
65 printf("Files in archive: %zu\n", nfiles);
68 for (i = 0; i < nfiles; i++) {
69 struct lbx_statbuf stat;
73 lbx_stat(lbx, i, &stat);
74 of = fopen(stat.name, "wbx");
76 errmsg("failed to create output file %s: %m.\n",
81 if (verbose) printf("extracting %s...\n", stat.name);
82 rc = lbx_extract(lbx, i, of);
84 if (rc == 0) remove(stat.name);
85 errmsg("error extracting %s: %s.\n",
86 stat.name, lbx_strerror());
89 if (verbose) printf("wrote %zu bytes.\n", rc);
96 int main(int argc, char **argv)
98 int mode = MODE_NONE, verbose = 0;
99 const char *name = "stdin";
103 static const char *sopts = "lxf:i:v";
104 static const struct option lopts[] = {
105 { "list", 0, NULL, 'l' },
106 { "extract", 0, NULL, 'x' },
108 { "file", 1, NULL, 'f' },
109 { "index", 1, NULL, 'i' },
111 { "verbose", 0, NULL, 'v' },
116 progname = "lbxtool"; /* argv[0]; */
117 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
126 if (strcmp(optarg, "-") == 0)
129 name = strrchr(optarg, '/');
130 name = name ? name+1 : optarg;
132 f = fopen(optarg, "rb");
134 errmsg("failed to open file %s: %m\n", optarg);
139 /* FIXME: Add index file support. */
151 return list(f, name, verbose);
153 return extract(f, name, verbose);
156 fprintf(stderr, "%s: you must specify a mode.\n", progname);