]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbxtool.c
Implement stub list operation.
[liblbx.git] / src / lbxtool.c
index f9a17d10e9371e7e7aadbf6f011187d6731102de..a8b899ac80a10d4d0c7317b6d122e371b8d62d12 100644 (file)
@@ -7,7 +7,7 @@
 
 static const char *progname;
 #define errmsg(fmt, ...) (\
-       fprintf(stderr, "%s: " fmt ": %m.\n", progname, __VA_ARGS__)\
+       fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
 )
 
 enum {
@@ -16,23 +16,40 @@ enum {
        MODE_EXTRACT,
 };
 
+int list(FILE *f, int verbose) {
+       LBX *lbx = lbx_fopen(f);
+       if (!lbx) {
+               errmsg("failed to open archive: %s.\n", lbx_strerror());
+               return EXIT_FAILURE;
+       }
+
+       if (verbose) {
+               printf("Files in archive: %zd\n", lbx_numfiles(lbx));
+       }
+
+       lbx_close(lbx);
+}
+
 int main(int argc, char **argv)
 {
-       int mode = MODE_NONE;
+       int mode = MODE_NONE, verbose = 0;
        FILE *f  = stdin;
        int opt;
 
-       struct option longopts[] = {
+       static const char         *sopts   = "lxf:v";
+       static const struct option lopts[] = {
                { "list",    0, NULL, 'l' },
                { "extract", 0, NULL, 'x' },
 
                { "file",    1, NULL, 'f' },
 
+               { "verbose", 0, NULL, 'v' },
+
                { 0 }
        };
 
-       progname = argv[0];
-       while ((opt = getopt_long(argc, argv, "lxf:", longopts, NULL)) != -1) {
+       progname = "lbxtool"; /* argv[0]; */
+       while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
                switch(opt) {
                case 'l':
                        mode = MODE_LIST;
@@ -43,17 +60,25 @@ int main(int argc, char **argv)
                case 'f':
                        f = fopen(optarg, "rb");
                        if (!f) {
-                               errmsg("failed to open file", 0, 4);
+                               errmsg("failed to open file %s: %m\n", optarg);
                                return EXIT_FAILURE;
                        }
                        break;
+               case 'v':
+                       verbose = 1;
+                       break;
                default:
                        return EXIT_FAILURE;
                }
        }
 
-       if (mode == MODE_NONE) {
-               fprintf(stderr, "%s: you must specify a mode.\n", progname);
-               return EXIT_FAILURE;
+       switch (mode) {
+       case MODE_LIST:
+               return list(f, verbose);
+       case MODE_EXTRACT:
+               return EXIT_SUCCESS;
        }
+
+       fprintf(stderr, "%s: you must specify a mode.\n", progname);
+       return EXIT_FAILURE;
 }