+ for (i = 0; i < nfiles; i++) {
+ struct lbx_statbuf stat;
+
+ lbx_stat(lbx, i, &stat);
+
+ switch (filematch(argv, stat.name)) {
+ case -1: continue;
+ case 0: break;
+ default: return EXIT_FAILURE;
+ }
+
+ printf("%s", stat.name);
+ if (verbose) {
+ printf(" size=%zu bytes", stat.size);
+ }
+
+ putchar('\n');
+ }
+
+ return EXIT_SUCCESS;
+}
+
+int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
+{
+ int ret = -1;
+ size_t rc;
+ FILE *of;
+
+ of = fopen(stat->name, "wb");
+ if (!of) {
+ errmsg("%s: fopen: %s\n",
+ stat->name, strerror(errno));
+ return -1;
+ }
+
+ while (1) {
+ unsigned char buf[1024];
+
+ rc = lbx_file_read(f, buf, sizeof buf);
+ if (rc == 0) {
+ if (lbx_file_eof(f))
+ ret = 0;
+ break;
+ }
+
+ if (fwrite(buf, rc, 1, of) != 1) {
+ errmsg("%s: fwrite: %s\n", stat->name, strerror(errno));
+ break;
+ }
+
+ if (rc < sizeof buf) {
+ if (lbx_file_eof(f))
+ ret = 0;
+ break;
+ }
+ }
+
+ if (fclose(of) == EOF) {
+ errmsg("%s: fclose: %s\n", stat->name, strerror(errno));
+ return -1;
+ }
+
+ return ret;
+}
+
+int extract(LBX *lbx, int verbose, char **argv) {
+ size_t nfiles;
+ unsigned int i;
+
+ nfiles = lbx_numfiles(lbx);
+ if (verbose) {
+ printf("Files in archive: %zu\n", nfiles);
+ }
+
+ for (i = 0; i < nfiles; i++) {
+ struct lbx_statbuf stat;
+ LBXfile *file;
+
+ lbx_stat(lbx, i, &stat);
+
+ switch (filematch(argv, stat.name)) {
+ case -1: continue;
+ case 0: break;
+ default: return EXIT_FAILURE;
+ }
+
+ file = lbx_file_open(lbx, i);
+ if (!file) {
+ errmsg("failed to open archive member %s: %s.\n",
+ stat.name, lbx_strerror());
+ continue;
+ }
+
+ if (extract_file(file, &stat) == -1)
+ return EXIT_FAILURE;
+ lbx_file_close(file);
+ }
+
+ return EXIT_SUCCESS;