]> granicus.if.org Git - zziplib/blob - bins/unzzipdir-mem.c
~0.13.64~ on github releases
[zziplib] / bins / unzzipdir-mem.c
1 /*
2  *      Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
3  *      Use freely under the restrictions of the ZLIB license.
4  *
5  *      This file is used as an example to clarify zzipmmap api usage.
6  */
7
8 #include <zzip/memdisk.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #ifdef ZZIP_HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #ifdef ZZIP_HAVE_IO_H
17 #include <io.h>
18 #endif
19
20 #ifdef ZZIP_HAVE_FNMATCH_H
21 #include <fnmatch.h>
22 #else
23 #define fnmatch(x,y,z) strcmp(x,y)
24 #endif
25
26 #ifndef O_BINARY
27 #define O_BINARY 0
28 #endif
29
30 static const char usage[] = 
31 {
32     "unzzipdir <zip> [names].. \n"
33     "  - unzzip a listing of files contained in a zip archive.\n"
34 };
35
36 int 
37 main (int argc, char ** argv)
38 {
39     int argn;
40     ZZIP_MEM_DISK* disk;
41
42     if (argc <= 1 || ! strcmp (argv[1], "--help"))
43     {
44         printf (usage);
45         return 0;
46     }
47     if (! strcmp (argv[1], "--version"))
48     {
49         printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
50         return 0;
51     }
52
53     disk = zzip_mem_disk_open (argv[1]);
54     if (! disk) {
55         perror(argv[1]);
56         return -1;
57     }
58
59     if (argc == 2)
60     {  /* list all */
61         ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
62         for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
63         {
64             char* name = zzip_mem_entry_to_name (entry);
65             printf ("%s\n", name);
66         }
67         return 0;
68     }
69
70     if (argc == 3)
71     {  /* list from one spec */
72         ZZIP_MEM_ENTRY* entry = 0;
73         while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0)))
74         {
75             char* name = zzip_mem_entry_to_name (entry);
76             printf ("%s\n", name);
77         }
78         return 0;
79     }
80
81     {   /* list only the matching entries - in order of zip directory */
82         ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
83         for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
84         {
85             char* name = zzip_mem_entry_to_name (entry);
86             for (argn=1; argn < argc; argn++)
87             {
88                 if (! fnmatch (argv[argn], name, 
89                                FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD))
90                     printf ("%s\n", name);
91             }
92         }
93         return 0;
94     }
95
96
97 /* 
98  * Local variables:
99  * c-file-style: "stroustrup"
100  * End:
101  */