]> git.draconx.ca Git - liblbx.git/blob - src/lbxtool.c
Add a name field to support LBX1 fallback.
[liblbx.git] / src / lbxtool.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <getopt.h>
6
7 #include "lbx.h"
8
9 static const char *progname;
10 #define errmsg(fmt, ...) (\
11         fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
12 )
13
14 enum {
15         MODE_NONE,
16         MODE_LIST,
17         MODE_EXTRACT,
18 };
19
20 int list(FILE *f, const char *name, int verbose) {
21         LBX *lbx = lbx_fopen(f, name);
22         if (!lbx) {
23                 errmsg("failed to open archive: %s.\n", lbx_strerror());
24                 return EXIT_FAILURE;
25         }
26
27         if (verbose) {
28                 printf("Files in archive: %zd\n", lbx_numfiles(lbx));
29         }
30
31         lbx_close(lbx);
32 }
33
34 int main(int argc, char **argv)
35 {
36         int mode = MODE_NONE, verbose = 0;
37         const char *name = "stdin";
38         FILE *f  = stdin;
39         int opt;
40
41         static const char         *sopts   = "lxf:i:v";
42         static const struct option lopts[] = {
43                 { "list",    0, NULL, 'l' },
44                 { "extract", 0, NULL, 'x' },
45
46                 { "file",    1, NULL, 'f' },
47                 { "index",   1, NULL, 'i' },
48
49                 { "verbose", 0, NULL, 'v' },
50
51                 { 0 }
52         };
53
54         progname = "lbxtool"; /* argv[0]; */
55         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
56                 switch(opt) {
57                 case 'l':
58                         mode = MODE_LIST;
59                         break;
60                 case 'x':
61                         mode = MODE_EXTRACT;
62                         break;
63                 case 'f':
64                         if (strcmp(optarg, "-") == 0)
65                                 break;
66
67                         name = strrchr(optarg, '/');
68                         name = name ? name+1 : optarg;
69
70                         f = fopen(optarg, "rb");
71                         if (!f) {
72                                 errmsg("failed to open file %s: %m\n", optarg);
73                                 return EXIT_FAILURE;
74                         }
75                         break;
76                 case 'i':
77                         /* FIXME: Add index file support. */
78                         break;
79                 case 'v':
80                         verbose = 1;
81                         break;
82                 default:
83                         return EXIT_FAILURE;
84                 }
85         }
86
87         switch (mode) {
88         case MODE_LIST:
89                 return list(f, name, verbose);
90         case MODE_EXTRACT:
91                 return EXIT_SUCCESS;
92         }
93
94         fprintf(stderr, "%s: you must specify a mode.\n", progname);
95         return EXIT_FAILURE;
96 }