Debian Codegraph @

libs/zziplib/zziplib-src/bins/unzzipdir-mix.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 zzip api usage.
6 */
7
8 #include <zzip/lib.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 switch (e)
31 {
32 case ZZIP_NO_ERROR:
33 return EXIT_OK;
34 case ZZIP_OUTOFMEM: /* out of memory */
35 return EXIT_ENOMEM;
36 case ZZIP_DIR_OPEN: /* failed to open zipfile, see errno for details */
37 return EXIT_ZIP_NOT_FOUND;
38 case ZZIP_DIR_STAT: /* failed to fstat zipfile, see errno for details */
39 case ZZIP_DIR_SEEK: /* failed to lseek zipfile, see errno for details */
40 case ZZIP_DIR_READ: /* failed to read zipfile, see errno for details */
41 case ZZIP_DIR_TOO_SHORT:
42 case ZZIP_DIR_EDH_MISSING:
43 return EXIT_FILEFORMAT;
44 case ZZIP_DIRSIZE:
45 return EXIT_EARLY_END_OF_FILE;
46 case ZZIP_ENOENT:
47 return EXIT_FILE_NOT_FOUND;
48 case ZZIP_UNSUPP_COMPR:
49 return EXIT_UNSUPPORTED_COMPRESSION;
50 case ZZIP_CORRUPTED:
51 case ZZIP_UNDEF:
52 case ZZIP_DIR_LARGEFILE:
53 return EXIT_FILEFORMAT;
54 }
55 return EXIT_ERRORS;
56 }
57
58 static int
59 unzzip_list (int argc, char ** argv, int verbose)
60 {
61 int argn;
62 ZZIP_DIR* disk;
63
64 if (argc == 1)
65 {
66 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
67 return EXIT_OK; /* better provide an archive argument */
68 }
69
70 disk = zzip_opendir (argv[1]);
71 if (! disk) {
72 DBG3("opendir failed [%i] %s", errno, strerror(errno));
73 perror(argv[1]);
74 return exitcode(errno);
75 }
76
77 if (argc == 2)
78 { /* list all */
79 ZZIP_DIRENT* entry = 0;
80 while((entry = zzip_readdir(disk)))
81 {
82 char* name = entry->d_name;
83 long long usize = entry->st_size;
84 if (!verbose)
85 {
86 printf ("%22lli %s\n", usize, name);
87 } else
88 {
89 long long csize = entry->d_csize;
90 unsigned compr = entry->d_compr;
91 const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)";
92 printf ("%lli/%lli %s %s\n", usize, csize, defl, name);
93 }
94 }
95 DBG2("readdir done %s", strerror(errno));
96 }
97 else
98 { /* list only the matching entries - in order of zip directory */
99 ZZIP_DIRENT* entry = 0;
100 while((entry = zzip_readdir(disk)))
101 {
102 char* name = entry->d_name;
103 for (argn=1; argn < argc; argn++)
104 {
105 if (! _zzip_fnmatch (argv[argn], name,
106 _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
107 {
108 long long usize = entry->st_size;
109 if (!verbose)
110 {
111 printf ("%22lli %s\n", usize, name);
112 } else
113 {
114 long long csize = entry->d_csize;
115 unsigned compr = entry->d_compr;
116 const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)";
117 printf ("%lli/%lli %s %s\n", usize, csize, defl, name);
118 }
119 break; /* match loop */
120 }
121 }
122 }
123 DBG2("readdir done %s", strerror(errno));
124 }
125 zzip_closedir(disk);
126 return 0;
127 }
128
129 int
130 unzzip_long_list (int argc, char ** argv)
131 {
132 return unzzip_list(argc, argv, 1);
133 }
134
135 int
136 unzzip_show_list (int argc, char ** argv)
137 {
138 return unzzip_list(argc, argv, 0);
139 }
140
141 /*
142 * Local variables:
143 * c-file-style: "stroustrup"
144 * End:
145 */