Debian Codegraph @

libs/zziplib/zziplib-src/bins/unzzipcat-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 <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <zzip/__mkdir.h>
14 #include <zzip/__fnmatch.h>
15 #include <zzip/__string.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_mem_entry_fprint(ZZIP_MEM_DISK* disk,
32 ZZIP_MEM_ENTRY* entry, FILE* out)
33 {
34 ZZIP_DISK_FILE* file = zzip_mem_entry_fopen (disk, entry);
35 if (file)
36 {
37 char buffer[1024]; int len;
38 while (0 < (len = zzip_mem_disk_fread (buffer, 1024, 1, file)))
39 fwrite (buffer, len, 1, out);
40
41 zzip_mem_disk_fclose (file);
42 }
43 }
44
45 static void unzzip_mem_disk_cat_file(ZZIP_MEM_DISK* disk, char* name, FILE* out)
46 {
47 ZZIP_DISK_FILE* file = zzip_mem_disk_fopen (disk, name);
48 if (file)
49 {
50 char buffer[1025]; int len;
51 while (0 < (len = zzip_mem_disk_fread (buffer, 1, 1024, file)))
52 {
53 fwrite (buffer, 1, len, out);
54 }
55
56 zzip_mem_disk_fclose (file);
57 }
58 }
59
60 static int unzzip_cat (int argc, char ** argv, int extract)
61 {
62 int done = 0;
63 int argn;
64 ZZIP_MEM_DISK* disk;
65
66 if (argc == 1)
67 {
68 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
69 return EXIT_OK; /* better provide an archive argument */
70 }
71
72 disk = zzip_mem_disk_open (argv[1]);
73 if (! disk) {
74 DBG3("disk_open failed [%i] %s", errno, strerror(errno));
75 perror(argv[1]);
76 return exitcode(errno);
77 }
78
79 if (argc == 2)
80 { /* print directory list */
81 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
82 DBG2("findfirst %p\n", entry);
83 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
84 {
85 char* name = zzip_mem_entry_to_name (entry);
86 FILE* out = stdout;
87 if (extract) out = create_fopen(name, "wb", 1);
88 if (! out) {
89 if (errno != EISDIR) {
90 DBG3("can not open output file %i %s", errno, strerror(errno));
91 done = EXIT_ERRORS;
92 }
93 continue;
94 }
95 unzzip_mem_disk_cat_file (disk, name, out);
96 if (extract) fclose(out);
97 }
98 }
99 else if (argc == 3 && !extract)
100 { /* list from one spec */
101 ZZIP_MEM_ENTRY* entry = 0;
102 while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0)))
103 {
104 unzzip_mem_entry_fprint (disk, entry, stdout);
105 }
106 } else {
107 for (argn=1; argn < argc; argn++)
108 { /* list only the matching entries - each in order of commandline */
109 ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
110 for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
111 {
112 char* name = zzip_mem_entry_to_name (entry);
113 if (! _zzip_fnmatch (argv[argn], name,
114 _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
115 {
116 FILE* out = stdout;
117 if (extract) out = create_fopen(name, "wb", 1);
118 if (! out) {
119 if (errno != EISDIR) {
120 DBG3("can not open output file %i %s", errno, strerror(errno));
121 done = EXIT_ERRORS;
122 }
123 continue;
124 }
125 unzzip_mem_disk_cat_file (disk, name, out);
126 if (extract) fclose(out);
127 break; /* match loop */
128 }
129 }
130 }
131 }
132 zzip_mem_disk_close(disk);
133 return done;
134 }
135
136 int unzzip_print (int argc, char ** argv)
137 {
138 return unzzip_cat(argc, argv, 0);
139 }
140
141 int unzzip_extract (int argc, char ** argv)
142 {
143 return unzzip_cat(argc, argv, 1);
144 }
145
146 /*
147 * Local variables:
148 * c-file-style: "stroustrup"
149 * End:
150 */