beta-0.89.2
[luatex.git] / source / libs / zziplib / zziplib-0.13.62 / bins / unzzipcat-mem.c
blob7474854187eeab5d822b285833841c3a9994b89e
1 /*
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.
6 */
8 #include <zzip/memdisk.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
13 #ifdef ZZIP_HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #ifdef ZZIP_HAVE_IO_H
17 #include <io.h>
18 #endif
20 #ifdef ZZIP_HAVE_FNMATCH_H
21 #include <fnmatch.h>
22 #else
23 #define fnmatch(x,y,z) strcmp(x,y)
24 #endif
26 #ifndef O_BINARY
27 #define O_BINARY 0
28 #endif
30 static const char usage[] =
32 "unzzipdir-mem <zip> [names].. \n"
33 " - unzzip data content of files contained in a zip archive.\n"
36 static void zzip_mem_entry_fprint(ZZIP_MEM_DISK* disk,
37 ZZIP_MEM_ENTRY* entry, FILE* out)
39 ZZIP_DISK_FILE* file = zzip_mem_entry_fopen (disk, entry);
40 if (file)
42 char buffer[1024]; int len;
43 while ((len = zzip_mem_disk_fread (buffer, 1024, 1, file)))
44 fwrite (buffer, len, 1, out);
46 zzip_mem_disk_fclose (file);
50 static void zzip_mem_disk_cat_file(ZZIP_MEM_DISK* disk, char* name, FILE* out)
52 ZZIP_DISK_FILE* file = zzip_mem_disk_fopen (disk, name);
53 if (file)
55 char buffer[1024]; int len;
56 while ((len = zzip_mem_disk_fread (buffer, 1, 1024, file)))
58 fwrite (buffer, 1, len, out);
61 zzip_mem_disk_fclose (file);
65 int
66 main (int argc, char ** argv)
68 int argn;
69 ZZIP_MEM_DISK* disk;
71 if (argc <= 1 || ! strcmp (argv[1], "--help"))
73 printf (usage);
74 return 0;
76 if (! strcmp (argv[1], "--version"))
78 printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
79 return 0;
82 disk = zzip_mem_disk_open (argv[1]);
83 if (! disk) {
84 perror(argv[1]);
85 return -1;
88 if (argc == 2)
89 { /* print directory list */
90 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
91 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
93 char* name = zzip_mem_entry_to_name (entry);
94 printf ("%s\n", name);
96 return 0;
99 if (argc == 3)
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 zzip_mem_entry_fprint (disk, entry, stdout);
107 return 0;
110 for (argn=1; argn < argc; argn++)
111 { /* list only the matching entries - each in order of commandline */
112 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
113 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
115 char* name = zzip_mem_entry_to_name (entry);
116 if (! fnmatch (argv[argn], name,
117 FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD))
118 zzip_mem_disk_cat_file (disk, name, stdout);
121 return 0;
125 * Local variables:
126 * c-file-style: "stroustrup"
127 * End: