Debian Codegraph @

libs/zziplib/zziplib-src/bins/unzzipcat-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 <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <zzip/__mkdir.h>
14 #include <zzip/__string.h>
15 #include <zzip/__fnmatch.h>
16 #include <zzip/__debug.h>
17 #include "unzzipcat-zip.h"
18 #include "unzzip-states.h"
19
20 #ifdef ZZIP_HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 #ifdef ZZIP_HAVE_IO_H
24 #include <io.h>
25 #endif
26
27 /* Functions in unzzip.c: */
28 extern int exitcode(int);
29 extern FILE* create_fopen(char*, char*, int);
30
31 static void unzzip_cat_file(ZZIP_DIR* disk, char* name, FILE* out)
32 {
33 ZZIP_FILE* file = zzip_fopen(name, "rb");
34 if (file)
35 {
36 char buffer[1024]; int len;
37 while (0 < (len = zzip_fread (buffer, 1, 1024, file)))
38 {
39 fwrite (buffer, 1, len, out);
40 }
41
42 zzip_fclose (file);
43 }
44 }
45
46 static int unzzip_cat (int argc, char ** argv, int extract)
47 {
48 int done = 0;
49 int argn;
50 ZZIP_DIR* disk;
51
52 if (argc == 1)
53 {
54 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
55 return EXIT_OK; /* better provide an archive argument */
56 }
57
58 disk = zzip_opendir (argv[1]);
59 if (! disk) {
60 DBG3("opendir failed [%i] %s", errno, strerror(errno));
61 perror(argv[1]);
62 return exitcode(errno);
63 }
64
65 if (argc == 2)
66 { /* list all */
67 ZZIP_DIRENT* entry = 0;
68 while((entry = zzip_readdir(disk)))
69 {
70 char* name = entry->d_name;
71 FILE* out = stdout;
72 if (extract) out = create_fopen(name, "wb", 1);
73 if (! out) {
74 if (errno != EISDIR) done = EXIT_ERRORS;
75 continue;
76 }
77 unzzip_cat_file (disk, name, out);
78 if (extract) fclose(out);
79 }
80 DBG2("readdir done %s", strerror(errno));
81 }
82 else
83 { /* list only the matching entries - in order of zip directory */
84 ZZIP_DIRENT* entry = 0;
85 while((entry = zzip_readdir(disk)))
86 {
87 char* name = entry->d_name;
88 for (argn=1; argn < argc; argn++)
89 {
90 if (! _zzip_fnmatch (argv[argn], name,
91 _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
92 {
93 FILE* out = stdout;
94 char* zip_name = argv[1];
95 int zip_name_len = strlen(zip_name);
96 int name_len = strlen(name);
97 char* mix_name = malloc(zip_name_len + 2 + name_len);
98 if (zip_name_len > 4 && !strcmp(zip_name+zip_name_len-4, ".zip"))
99 zip_name_len -= 4;
100 memcpy(mix_name, zip_name, zip_name_len);
101 mix_name[zip_name_len] = '/';
102 strcpy(mix_name + zip_name_len + 1, name);
103 if (extract) out = create_fopen(name, "wb", 1);
104 if (! out) {
105 if (errno != EISDIR) done = EXIT_ERRORS;
106 continue;
107 }
108 fprintf(stderr, "%s %s -> %s\n", zip_name, name, mix_name);
109 /* 'test1.zip' 'README' -> 'test1/README' */
110 unzzip_cat_file (disk, mix_name, out);
111 if (extract) fclose(out);
112 break; /* match loop */
113 }
114 }
115 }
116 }
117 zzip_closedir(disk);
118 return done;
119 }
120
121 int unzzip_print (int argc, char ** argv)
122 {
123 return unzzip_cat(argc, argv, 0);
124 }
125
126 int unzzip_extract (int argc, char ** argv)
127 {
128 return unzzip_cat(argc, argv, 1);
129 }
130
131 /*
132 * Local variables:
133 * c-file-style: "stroustrup"
134 * End:
135 */