#define _GNU_SOURCE #include #include #include #include "lbx.h" static const char *progname; #define errmsg(fmt, ...) (\ fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\ ) enum { MODE_NONE, MODE_LIST, 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, verbose = 0; FILE *f = stdin; int opt; 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 = "lbxtool"; /* argv[0]; */ while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { switch(opt) { case 'l': mode = MODE_LIST; break; case 'x': mode = MODE_EXTRACT; break; case 'f': f = fopen(optarg, "rb"); if (!f) { errmsg("failed to open file %s: %m\n", optarg); return EXIT_FAILURE; } break; case 'v': verbose = 1; break; default: 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; }