10 static const char *progname;
11 #define errmsg(fmt, ...) (\
12 fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
21 int filematch(char **argv, const char *name)
25 for (i = 0; argv[i]; i++) {
26 switch(fnmatch(argv[i], name, 0)) {
32 errmsg("error matching glob: %s.\n", argv[i]);
40 int list(FILE *f, const char *name, int verbose, char **argv) {
45 lbx = lbx_fopen(f, name);
47 errmsg("failed to open archive: %s.\n", lbx_strerror());
51 nfiles = lbx_numfiles(lbx);
53 printf("Files in archive: %zu\n", nfiles);
56 for (i = 0; i < nfiles; i++) {
57 struct lbx_statbuf stat;
59 lbx_stat(lbx, i, &stat);
61 switch (filematch(argv, stat.name)) {
67 printf("%s", stat.name);
69 printf(" size=%zu bytes", stat.size);
82 int extract(FILE *f, const char *name, int verbose, char **argv) {
87 lbx = lbx_fopen(f, name);
89 errmsg("failed to open archive: %s.\n", lbx_strerror());
93 nfiles = lbx_numfiles(lbx);
95 printf("Files in archive: %zu\n", nfiles);
98 for (i = 0; i < nfiles; i++) {
99 struct lbx_statbuf stat;
104 lbx_stat(lbx, i, &stat);
106 switch (filematch(argv, stat.name)) {
112 of = fopen(stat.name, "wbx");
114 errmsg("failed to create output file %s: %m.\n",
119 if (verbose) printf("extracting %s...\n", stat.name);
120 rc = lbx_extract(lbx, i, of);
121 if (rc < stat.size) {
122 if (rc == 0) remove(stat.name);
123 errmsg("error extracting %s: %s.\n",
124 stat.name, lbx_strerror());
127 if (verbose) printf("wrote %zu bytes.\n", rc);
137 int main(int argc, char **argv)
139 int mode = MODE_NONE, verbose = 0;
140 const char *name = "stdin";
144 static const char *sopts = "lxf:i:v";
145 static const struct option lopts[] = {
146 { "list", 0, NULL, 'l' },
147 { "extract", 0, NULL, 'x' },
149 { "file", 1, NULL, 'f' },
150 { "index", 1, NULL, 'i' },
152 { "verbose", 0, NULL, 'v' },
157 progname = "lbxtool"; /* argv[0]; */
158 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
167 if (strcmp(optarg, "-") == 0)
170 name = strrchr(optarg, '/');
171 name = name ? name+1 : optarg;
173 f = fopen(optarg, "rb");
175 errmsg("failed to open file %s: %m\n", optarg);
180 /* FIXME: Add index file support. */
192 return list(f, name, verbose, &argv[optind]);
194 return extract(f, name, verbose, &argv[optind]);
197 fprintf(stderr, "%s: you must specify a mode.\n", progname);