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