2 * Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
3 * Use freely under the restrictions of the ZLIB license.
5 * This file is used as an example to clarify zzipmmap api usage.
8 #include <zzip/memdisk.h>
13 #include <zzip/__mkdir.h>
14 #include <zzip/__fnmatch.h>
15 #include <zzip/__string.h>
16 #include <zzip/__debug.h>
17 #include "unzzipcat-zip.h"
18 #include "unzzip-states.h"
20 #ifdef ZZIP_HAVE_UNISTD_H
27 /* Functions in unzzip.c: */
28 extern int exitcode(int);
29 extern FILE* create_fopen(char*, char*, int);
31 static void unzzip_mem_entry_fprint(ZZIP_MEM_DISK* disk,
32 ZZIP_MEM_ENTRY* entry, FILE* out)
34 ZZIP_DISK_FILE* file = zzip_mem_entry_fopen (disk, entry);
37 char buffer[1024]; int len;
38 while ((len = zzip_mem_disk_fread (buffer, 1024, 1, file)))
39 fwrite (buffer, len, 1, out);
41 zzip_mem_disk_fclose (file);
45 static void unzzip_mem_disk_cat_file(ZZIP_MEM_DISK* disk, char* name, FILE* out)
47 ZZIP_DISK_FILE* file = zzip_mem_disk_fopen (disk, name);
50 char buffer[1025]; int len;
51 while ((len = zzip_mem_disk_fread (buffer, 1, 1024, file)))
53 fwrite (buffer, 1, len, out);
56 zzip_mem_disk_fclose (file);
60 static int unzzip_cat (int argc, char ** argv, int extract)
68 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
69 return EXIT_OK; /* better provide an archive argument */
72 disk = zzip_mem_disk_open (argv[1]);
74 DBG3("disk_open failed [%i] %s", errno, strerror(errno));
76 return exitcode(errno);
80 { /* print directory list */
81 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
82 DBG2("findfirst %p\n", entry);
83 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
85 char* name = zzip_mem_entry_to_name (entry);
87 if (extract) out = create_fopen(name, "wb", 1);
89 if (errno != EISDIR) {
90 DBG3("can not open output file %i %s", errno, strerror(errno));
95 unzzip_mem_disk_cat_file (disk, name, out);
96 if (extract) fclose(out);
99 else if (argc == 3 && !extract)
100 { /* list from one spec */
101 ZZIP_MEM_ENTRY* entry = 0;
102 while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0)))
104 unzzip_mem_entry_fprint (disk, entry, stdout);
107 for (argn=1; argn < argc; argn++)
108 { /* list only the matching entries - each in order of commandline */
109 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
110 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
112 char* name = zzip_mem_entry_to_name (entry);
113 if (! _zzip_fnmatch (argv[argn], name,
114 _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
117 if (extract) out = create_fopen(name, "wb", 1);
119 if (errno != EISDIR) {
120 DBG3("can not open output file %i %s", errno, strerror(errno));
125 unzzip_mem_disk_cat_file (disk, name, out);
126 if (extract) fclose(out);
127 break; /* match loop */
132 zzip_mem_disk_close(disk);
136 int unzzip_print (int argc, char ** argv)
138 return unzzip_cat(argc, argv, 0);
141 int unzzip_extract (int argc, char ** argv)
143 return unzzip_cat(argc, argv, 1);
148 * c-file-style: "stroustrup"