Debian Codegraph @

libs/zziplib/zziplib-src/bins/unzzip.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 <sys/stat.h>
9 #include <zzip/zzip.h>
10 #include <zzip/__string.h>
11 #include <zzip/__mkdir.h>
12 #include <zzip/__debug.h>
13 #include <zzip/file.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "unzzipcat-zip.h"
18 #include "unzzipdir-zip.h"
19 #include "unzzip-states.h"
20
21 static const char usage[] =
22 {
23 "unzzip <dir>.. \n"
24 " - unzzip the files contained in a zip archive.\n"
25 " -p print content of files to pipe\n"
26 " -l list names in archive (short format)\n"
27 };
28
29 static int unzzip_version(void)
30 {
31 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
32 return 0;
33 }
34
35 static int unzzip_help(void)
36 {
37 printf (usage);
38 return 0;
39 }
40
41 /* Functions used by unzzipcat-*.c: */
42 int exitcode(int e)
43 {
44 switch (e)
45 {
46 case ZZIP_NO_ERROR:
47 return EXIT_OK;
48 case ZZIP_OUTOFMEM: /* out of memory */
49 return EXIT_ENOMEM;
50 case ZZIP_DIR_OPEN: /* failed to open zipfile, see errno for details */
51 return EXIT_ZIP_NOT_FOUND;
52 case ZZIP_DIR_STAT: /* failed to fstat zipfile, see errno for details */
53 case ZZIP_DIR_SEEK: /* failed to lseek zipfile, see errno for details */
54 case ZZIP_DIR_READ: /* failed to read zipfile, see errno for details */
55 case ZZIP_DIR_TOO_SHORT:
56 case ZZIP_DIR_EDH_MISSING:
57 return EXIT_FILEFORMAT;
58 case ZZIP_DIRSIZE:
59 return EXIT_EARLY_END_OF_FILE;
60 case ZZIP_ENOENT:
61 return EXIT_FILE_NOT_FOUND;
62 case ZZIP_UNSUPP_COMPR:
63 return EXIT_UNSUPPORTED_COMPRESSION;
64 case ZZIP_CORRUPTED:
65 case ZZIP_UNDEF:
66 case ZZIP_DIR_LARGEFILE:
67 return EXIT_FILEFORMAT;
68 }
69 return EXIT_ERRORS;
70 }
71
72 /*
73 * NAME: remove_dotdotslash
74 * PURPOSE: To remove any "../" components from the given pathname
75 * ARGUMENTS: path: path name with maybe "../" components
76 * RETURNS: Nothing, "path" is modified in-place
77 * NOTE: removing "../" from the path ALWAYS shortens the path, never adds to it!
78 * Also, "path" is not used after creating it.
79 * So modifying "path" in-place is safe to do.
80 */
81 static inline void
82 remove_dotdotslash(char *path)
83 {
84 /* Note: removing "../" from the path ALWAYS shortens the path, never adds to it! */
85 char *dotdotslash;
86 int warned = 0;
87
88 dotdotslash = path;
89 while ((dotdotslash = strstr(dotdotslash, "../")) != NULL)
90 {
91 /*
92 * Remove only if at the beginning of the pathname ("../path/name")
93 * or when preceded by a slash ("path/../name"),
94 * otherwise not ("path../name..")!
95 */
96 if (dotdotslash == path || dotdotslash[-1] == '/')
97 {
98 char *src, *dst;
99 if (!warned)
100 {
101 /* Note: the first time through the pathname is still intact */
102 fprintf(stderr, "Removing \"../\" path component(s) in %s\n", path);
103 warned = 1;
104 }
105 /* We cannot use strcpy(), as there "The strings may not overlap" */
106 for (src = dotdotslash+3, dst=dotdotslash; (*dst = *src) != '\0'; src++, dst++)
107 ;
108 }
109 else
110 dotdotslash +=3; /* skip this instance to prevent infinite loop */
111 }
112 }
113
114 static void makedirs(const char* name)
115 {
116 char* p = strrchr(name, '/');
117 if (p) {
118 char* dir_name = _zzip_strndup(name, p-name);
119 makedirs(dir_name);
120 free (dir_name);
121 }
122 if (_zzip_mkdir(name, 0775) == -1 && errno != EEXIST)
123 {
124 DBG3("while mkdir %s : %s", name, strerror(errno));
125 }
126 errno = 0;
127 }
128
129 FILE* create_fopen(char* name, char* mode, int subdirs)
130 {
131 char name_stripped[PATH_MAX];
132
133 strncpy(name_stripped, name, PATH_MAX);
134 remove_dotdotslash(name_stripped);
135
136 if (subdirs)
137 {
138 char* p = strrchr(name_stripped, '/');
139 if (p) {
140 char* dir_name = _zzip_strndup(name_stripped, p-name);
141 makedirs(dir_name);
142 free (dir_name);
143 }
144 }
145 return fopen(name_stripped, mode);
146 }
147
148 int
149 main (int argc, char ** argv)
150 {
151 int argn;
152 int exitcode = 0;
153 zzip_error_t error;
154
155 if (argc <= 1 || ! strcmp (argv[1], "--help"))
156 {
157 return unzzip_help();
158 }
159 if (! strcmp (argv[1], "--version"))
160 {
161 return unzzip_version();
162 }
163 if (! strcmp (argv[1], "-l") || ! strcmp(argv[1], "--list"))
164 {
165 argc -= 1; argv += 1;
166 return unzzip_show_list(argc, argv);
167 }
168 if (! strcmp (argv[1], "-v") || ! strcmp(argv[1], "--versions"))
169 {
170 if (argc == 2)
171 return unzzip_version(); /* compatible with info-zip */
172 argc -= 1; argv += 1;
173 return unzzip_long_list(argc, argv);
174 }
175 if (! strcmp (argv[1], "-p") || ! strcmp(argv[1], "--pipe"))
176 {
177 argc -= 1; argv += 1;
178 return unzzip_print(argc, argv);
179 }
180
181 if (! strcmp (argv[1], "-"))
182 {
183 fprintf(stderr, "unknown option %s", argv[1]);
184 return EXIT_INVALID_OPTION;
185 }
186
187 return unzzip_extract(argc, argv);
188 }
189
190 /*
191 * Local variables:
192 * c-file-style: "stroustrup"
193 * End:
194 */