Debian Codegraph @

libs/zziplib/zziplib-src/bins/unzzipdir-mem.c

raw
1 /*
2 * Copyright (c) 2003 Guido Draheim <[email protected]>
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 <zzip/__debug.h>
10 #include <zzip/__fnmatch.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "unzzipdir-zip.h"
15 #include "unzzip-states.h"
16
17 #ifdef ZZIP_HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #ifdef ZZIP_HAVE_IO_H
21 #include <io.h>
22 #endif
23
24 static const char* comprlevel[] = {
25 "stored", "shrunk", "redu:1", "redu:2", "redu:3", "redu:4",
26 "impl:N", "toknze", "defl:N", "defl:B", "impl:B" };
27
28 static int exitcode(int e)
29 {
30 return EXIT_ERRORS;
31 }
32
33 static int
34 unzzip_list (int argc, char ** argv, int verbose)
35 {
36 int argn;
37 ZZIP_MEM_DISK* disk;
38
39 if (argc == 1)
40 {
41 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
42 return EXIT_OK; /* better provide an archive argument */
43 }
44
45 disk = zzip_mem_disk_open (argv[1]);
46 if (! disk) {
47 DBG3("disk_open failed [%i] %s", errno, strerror(errno));
48 perror(argv[1]);
49 return exitcode(errno);
50 }
51
52 if (argc == 2)
53 { /* list all */
54 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
55 DBG2("findfirst %p", entry);
56 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
57 {
58 char* name = zzip_mem_entry_to_name (entry);
59 long long usize = entry->zz_usize;
60 if (!verbose)
61 {
62 printf ("%22lli %s\n", usize, name);
63 } else
64 {
65 long long csize = entry->zz_csize;
66 unsigned compr = entry->zz_compr;
67 const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)";
68 printf ("%lli/%lli %s %s\n", csize, usize, defl, name);
69 }
70 }
71 }
72 else if (argc == 3)
73 { /* list from one spec */
74 ZZIP_MEM_ENTRY* entry = 0;
75 while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0)))
76 {
77 char* name = zzip_mem_entry_to_name (entry);
78 long long usize = entry->zz_usize;
79 if (!verbose)
80 {
81 printf ("%22lli %s\n", usize, name);
82 } else
83 {
84 long long csize = entry->zz_csize;
85 unsigned compr = entry->zz_compr;
86 const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)";
87 printf ("%lli/%lli %s %s\n", csize, usize, defl, name);
88 }
89 }
90 }
91 else
92 { /* list only the matching entries - in order of zip directory */
93 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
94 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
95 {
96 char* name = zzip_mem_entry_to_name (entry);
97 for (argn=1; argn < argc; argn++)
98 {
99 if (! _zzip_fnmatch (argv[argn], name,
100 _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
101 {
102 char* name = zzip_mem_entry_to_name (entry);
103 long long usize = entry->zz_usize;
104 if (!verbose)
105 {
106 printf ("%22lli %s\n", usize, name);
107 } else
108 {
109 long long csize = entry->zz_csize;
110 unsigned compr = entry->zz_compr;
111 const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)";
112 printf ("%lli/%lli %s %s\n", csize, usize, defl, name);
113 }
114 break; /* match loop */
115 }
116 }
117 }
118 }
119 zzip_mem_disk_close(disk);
120 return EXIT_OK;
121 }
122
123 int
124 unzzip_long_list (int argc, char ** argv)
125 {
126 return unzzip_list(argc, argv, 1);
127 }
128
129 int
130 unzzip_show_list (int argc, char ** argv)
131 {
132 return unzzip_list(argc, argv, 0);
133 }
134
135 /*
136 * Local variables:
137 * c-file-style: "stroustrup"
138 * End:
139 */