1*40b23ce4SThomas Cort /* $NetBSD: unzip.c,v 1.19 2011/09/06 18:43:41 joerg Exp $ */
2*40b23ce4SThomas Cort
3*40b23ce4SThomas Cort /*-
4*40b23ce4SThomas Cort * Copyright (c) 2009, 2010 Joerg Sonnenberger <joerg@NetBSD.org>
5*40b23ce4SThomas Cort * Copyright (c) 2007-2008 Dag-Erling Co�dan Sm�rgrav
6*40b23ce4SThomas Cort * All rights reserved.
7*40b23ce4SThomas Cort *
8*40b23ce4SThomas Cort * Redistribution and use in source and binary forms, with or without
9*40b23ce4SThomas Cort * modification, are permitted provided that the following conditions
10*40b23ce4SThomas Cort * are met:
11*40b23ce4SThomas Cort * 1. Redistributions of source code must retain the above copyright
12*40b23ce4SThomas Cort * notice, this list of conditions and the following disclaimer
13*40b23ce4SThomas Cort * in this position and unchanged.
14*40b23ce4SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
15*40b23ce4SThomas Cort * notice, this list of conditions and the following disclaimer in the
16*40b23ce4SThomas Cort * documentation and/or other materials provided with the distribution.
17*40b23ce4SThomas Cort *
18*40b23ce4SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*40b23ce4SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*40b23ce4SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*40b23ce4SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*40b23ce4SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*40b23ce4SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*40b23ce4SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*40b23ce4SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*40b23ce4SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*40b23ce4SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*40b23ce4SThomas Cort * SUCH DAMAGE.
29*40b23ce4SThomas Cort *
30*40b23ce4SThomas Cort * $FreeBSD: revision 180124$
31*40b23ce4SThomas Cort *
32*40b23ce4SThomas Cort * This file would be much shorter if we didn't care about command-line
33*40b23ce4SThomas Cort * compatibility with Info-ZIP's UnZip, which requires us to duplicate
34*40b23ce4SThomas Cort * parts of libarchive in order to gain more detailed control of its
35*40b23ce4SThomas Cort * behaviour for the purpose of implementing the -n, -o, -L and -a
36*40b23ce4SThomas Cort * options.
37*40b23ce4SThomas Cort */
38*40b23ce4SThomas Cort
39*40b23ce4SThomas Cort #include <sys/cdefs.h>
40*40b23ce4SThomas Cort __RCSID("$NetBSD: unzip.c,v 1.19 2011/09/06 18:43:41 joerg Exp $");
41*40b23ce4SThomas Cort
42*40b23ce4SThomas Cort #include <sys/queue.h>
43*40b23ce4SThomas Cort #include <sys/stat.h>
44*40b23ce4SThomas Cort
45*40b23ce4SThomas Cort #include <ctype.h>
46*40b23ce4SThomas Cort #include <errno.h>
47*40b23ce4SThomas Cort #include <fcntl.h>
48*40b23ce4SThomas Cort #include <fnmatch.h>
49*40b23ce4SThomas Cort #include <stdarg.h>
50*40b23ce4SThomas Cort #include <stdio.h>
51*40b23ce4SThomas Cort #include <stdlib.h>
52*40b23ce4SThomas Cort #include <string.h>
53*40b23ce4SThomas Cort #include <unistd.h>
54*40b23ce4SThomas Cort
55*40b23ce4SThomas Cort #include <archive.h>
56*40b23ce4SThomas Cort #include <archive_entry.h>
57*40b23ce4SThomas Cort
58*40b23ce4SThomas Cort /* command-line options */
59*40b23ce4SThomas Cort static int a_opt; /* convert EOL */
60*40b23ce4SThomas Cort static int C_opt; /* match case-insensitively */
61*40b23ce4SThomas Cort static int c_opt; /* extract to stdout */
62*40b23ce4SThomas Cort static const char *d_arg; /* directory */
63*40b23ce4SThomas Cort static int f_opt; /* update existing files only */
64*40b23ce4SThomas Cort static int j_opt; /* junk directories */
65*40b23ce4SThomas Cort static int L_opt; /* lowercase names */
66*40b23ce4SThomas Cort static int n_opt; /* never overwrite */
67*40b23ce4SThomas Cort static int o_opt; /* always overwrite */
68*40b23ce4SThomas Cort static int p_opt; /* extract to stdout, quiet */
69*40b23ce4SThomas Cort static int q_opt; /* quiet */
70*40b23ce4SThomas Cort static int t_opt; /* test */
71*40b23ce4SThomas Cort static int u_opt; /* update */
72*40b23ce4SThomas Cort static int v_opt; /* verbose/list */
73*40b23ce4SThomas Cort static const char * y_str = ""; /* 4 digit year */
74*40b23ce4SThomas Cort
75*40b23ce4SThomas Cort /* time when unzip started */
76*40b23ce4SThomas Cort static time_t now;
77*40b23ce4SThomas Cort
78*40b23ce4SThomas Cort /* debug flag */
79*40b23ce4SThomas Cort static int unzip_debug;
80*40b23ce4SThomas Cort
81*40b23ce4SThomas Cort /* running on tty? */
82*40b23ce4SThomas Cort static int tty;
83*40b23ce4SThomas Cort
84*40b23ce4SThomas Cort /* convenience macro */
85*40b23ce4SThomas Cort /* XXX should differentiate between ARCHIVE_{WARN,FAIL,RETRY} */
86*40b23ce4SThomas Cort #define ac(call) \
87*40b23ce4SThomas Cort do { \
88*40b23ce4SThomas Cort int acret = (call); \
89*40b23ce4SThomas Cort if (acret != ARCHIVE_OK) \
90*40b23ce4SThomas Cort errorx("%s", archive_error_string(a)); \
91*40b23ce4SThomas Cort } while (0)
92*40b23ce4SThomas Cort
93*40b23ce4SThomas Cort /*
94*40b23ce4SThomas Cort * Indicates that last info() did not end with EOL. This helps error() et
95*40b23ce4SThomas Cort * al. avoid printing an error message on the same line as an incomplete
96*40b23ce4SThomas Cort * informational message.
97*40b23ce4SThomas Cort */
98*40b23ce4SThomas Cort static int noeol;
99*40b23ce4SThomas Cort
100*40b23ce4SThomas Cort /* fatal error message + errno */
101*40b23ce4SThomas Cort __dead __printflike(1, 2) static void
error(const char * fmt,...)102*40b23ce4SThomas Cort error(const char *fmt, ...)
103*40b23ce4SThomas Cort {
104*40b23ce4SThomas Cort va_list ap;
105*40b23ce4SThomas Cort
106*40b23ce4SThomas Cort if (noeol)
107*40b23ce4SThomas Cort fprintf(stdout, "\n");
108*40b23ce4SThomas Cort fflush(stdout);
109*40b23ce4SThomas Cort fprintf(stderr, "unzip: ");
110*40b23ce4SThomas Cort va_start(ap, fmt);
111*40b23ce4SThomas Cort vfprintf(stderr, fmt, ap);
112*40b23ce4SThomas Cort va_end(ap);
113*40b23ce4SThomas Cort fprintf(stderr, ": %s\n", strerror(errno));
114*40b23ce4SThomas Cort exit(1);
115*40b23ce4SThomas Cort }
116*40b23ce4SThomas Cort
117*40b23ce4SThomas Cort /* fatal error message, no errno */
118*40b23ce4SThomas Cort __dead __printflike(1, 2) static void
errorx(const char * fmt,...)119*40b23ce4SThomas Cort errorx(const char *fmt, ...)
120*40b23ce4SThomas Cort {
121*40b23ce4SThomas Cort va_list ap;
122*40b23ce4SThomas Cort
123*40b23ce4SThomas Cort if (noeol)
124*40b23ce4SThomas Cort fprintf(stdout, "\n");
125*40b23ce4SThomas Cort fflush(stdout);
126*40b23ce4SThomas Cort fprintf(stderr, "unzip: ");
127*40b23ce4SThomas Cort va_start(ap, fmt);
128*40b23ce4SThomas Cort vfprintf(stderr, fmt, ap);
129*40b23ce4SThomas Cort va_end(ap);
130*40b23ce4SThomas Cort fprintf(stderr, "\n");
131*40b23ce4SThomas Cort exit(1);
132*40b23ce4SThomas Cort }
133*40b23ce4SThomas Cort
134*40b23ce4SThomas Cort #if 0
135*40b23ce4SThomas Cort /* non-fatal error message + errno */
136*40b23ce4SThomas Cort __printflike(1, 2) static void
137*40b23ce4SThomas Cort warning(const char *fmt, ...)
138*40b23ce4SThomas Cort {
139*40b23ce4SThomas Cort va_list ap;
140*40b23ce4SThomas Cort
141*40b23ce4SThomas Cort if (noeol)
142*40b23ce4SThomas Cort fprintf(stdout, "\n");
143*40b23ce4SThomas Cort fflush(stdout);
144*40b23ce4SThomas Cort fprintf(stderr, "unzip: ");
145*40b23ce4SThomas Cort va_start(ap, fmt);
146*40b23ce4SThomas Cort vfprintf(stderr, fmt, ap);
147*40b23ce4SThomas Cort va_end(ap);
148*40b23ce4SThomas Cort fprintf(stderr, ": %s\n", strerror(errno));
149*40b23ce4SThomas Cort }
150*40b23ce4SThomas Cort #endif
151*40b23ce4SThomas Cort /* non-fatal error message, no errno */
152*40b23ce4SThomas Cort __printflike(1, 2) static void
warningx(const char * fmt,...)153*40b23ce4SThomas Cort warningx(const char *fmt, ...)
154*40b23ce4SThomas Cort {
155*40b23ce4SThomas Cort va_list ap;
156*40b23ce4SThomas Cort
157*40b23ce4SThomas Cort if (noeol)
158*40b23ce4SThomas Cort fprintf(stdout, "\n");
159*40b23ce4SThomas Cort fflush(stdout);
160*40b23ce4SThomas Cort fprintf(stderr, "unzip: ");
161*40b23ce4SThomas Cort va_start(ap, fmt);
162*40b23ce4SThomas Cort vfprintf(stderr, fmt, ap);
163*40b23ce4SThomas Cort va_end(ap);
164*40b23ce4SThomas Cort fprintf(stderr, "\n");
165*40b23ce4SThomas Cort }
166*40b23ce4SThomas Cort
167*40b23ce4SThomas Cort /* informational message (if not -q) */
168*40b23ce4SThomas Cort __printflike(1, 2) static void
info(const char * fmt,...)169*40b23ce4SThomas Cort info(const char *fmt, ...)
170*40b23ce4SThomas Cort {
171*40b23ce4SThomas Cort va_list ap;
172*40b23ce4SThomas Cort
173*40b23ce4SThomas Cort if (q_opt && !unzip_debug)
174*40b23ce4SThomas Cort return;
175*40b23ce4SThomas Cort va_start(ap, fmt);
176*40b23ce4SThomas Cort vfprintf(stdout, fmt, ap);
177*40b23ce4SThomas Cort va_end(ap);
178*40b23ce4SThomas Cort fflush(stdout);
179*40b23ce4SThomas Cort
180*40b23ce4SThomas Cort if (*fmt == '\0')
181*40b23ce4SThomas Cort noeol = 1;
182*40b23ce4SThomas Cort else
183*40b23ce4SThomas Cort noeol = fmt[strlen(fmt) - 1] != '\n';
184*40b23ce4SThomas Cort }
185*40b23ce4SThomas Cort
186*40b23ce4SThomas Cort /* debug message (if unzip_debug) */
187*40b23ce4SThomas Cort __printflike(1, 2) static void
debug(const char * fmt,...)188*40b23ce4SThomas Cort debug(const char *fmt, ...)
189*40b23ce4SThomas Cort {
190*40b23ce4SThomas Cort va_list ap;
191*40b23ce4SThomas Cort
192*40b23ce4SThomas Cort if (!unzip_debug)
193*40b23ce4SThomas Cort return;
194*40b23ce4SThomas Cort va_start(ap, fmt);
195*40b23ce4SThomas Cort vfprintf(stderr, fmt, ap);
196*40b23ce4SThomas Cort va_end(ap);
197*40b23ce4SThomas Cort fflush(stderr);
198*40b23ce4SThomas Cort
199*40b23ce4SThomas Cort if (*fmt == '\0')
200*40b23ce4SThomas Cort noeol = 1;
201*40b23ce4SThomas Cort else
202*40b23ce4SThomas Cort noeol = fmt[strlen(fmt) - 1] != '\n';
203*40b23ce4SThomas Cort }
204*40b23ce4SThomas Cort
205*40b23ce4SThomas Cort /* duplicate a path name, possibly converting to lower case */
206*40b23ce4SThomas Cort static char *
pathdup(const char * path)207*40b23ce4SThomas Cort pathdup(const char *path)
208*40b23ce4SThomas Cort {
209*40b23ce4SThomas Cort char *str;
210*40b23ce4SThomas Cort size_t i, len;
211*40b23ce4SThomas Cort
212*40b23ce4SThomas Cort len = strlen(path);
213*40b23ce4SThomas Cort while (len && path[len - 1] == '/')
214*40b23ce4SThomas Cort len--;
215*40b23ce4SThomas Cort if ((str = malloc(len + 1)) == NULL) {
216*40b23ce4SThomas Cort errno = ENOMEM;
217*40b23ce4SThomas Cort error("malloc()");
218*40b23ce4SThomas Cort }
219*40b23ce4SThomas Cort if (L_opt) {
220*40b23ce4SThomas Cort for (i = 0; i < len; ++i)
221*40b23ce4SThomas Cort str[i] = tolower((unsigned char)path[i]);
222*40b23ce4SThomas Cort } else {
223*40b23ce4SThomas Cort memcpy(str, path, len);
224*40b23ce4SThomas Cort }
225*40b23ce4SThomas Cort str[len] = '\0';
226*40b23ce4SThomas Cort
227*40b23ce4SThomas Cort return (str);
228*40b23ce4SThomas Cort }
229*40b23ce4SThomas Cort
230*40b23ce4SThomas Cort /* concatenate two path names */
231*40b23ce4SThomas Cort static char *
pathcat(const char * prefix,const char * path)232*40b23ce4SThomas Cort pathcat(const char *prefix, const char *path)
233*40b23ce4SThomas Cort {
234*40b23ce4SThomas Cort char *str;
235*40b23ce4SThomas Cort size_t prelen, len;
236*40b23ce4SThomas Cort
237*40b23ce4SThomas Cort prelen = prefix ? strlen(prefix) + 1 : 0;
238*40b23ce4SThomas Cort len = strlen(path) + 1;
239*40b23ce4SThomas Cort if ((str = malloc(prelen + len)) == NULL) {
240*40b23ce4SThomas Cort errno = ENOMEM;
241*40b23ce4SThomas Cort error("malloc()");
242*40b23ce4SThomas Cort }
243*40b23ce4SThomas Cort if (prefix) {
244*40b23ce4SThomas Cort memcpy(str, prefix, prelen); /* includes zero */
245*40b23ce4SThomas Cort str[prelen - 1] = '/'; /* splat zero */
246*40b23ce4SThomas Cort }
247*40b23ce4SThomas Cort memcpy(str + prelen, path, len); /* includes zero */
248*40b23ce4SThomas Cort
249*40b23ce4SThomas Cort return (str);
250*40b23ce4SThomas Cort }
251*40b23ce4SThomas Cort
252*40b23ce4SThomas Cort /*
253*40b23ce4SThomas Cort * Pattern lists for include / exclude processing
254*40b23ce4SThomas Cort */
255*40b23ce4SThomas Cort struct pattern {
256*40b23ce4SThomas Cort STAILQ_ENTRY(pattern) link;
257*40b23ce4SThomas Cort char pattern[];
258*40b23ce4SThomas Cort };
259*40b23ce4SThomas Cort
260*40b23ce4SThomas Cort STAILQ_HEAD(pattern_list, pattern);
261*40b23ce4SThomas Cort static struct pattern_list include = STAILQ_HEAD_INITIALIZER(include);
262*40b23ce4SThomas Cort static struct pattern_list exclude = STAILQ_HEAD_INITIALIZER(exclude);
263*40b23ce4SThomas Cort
264*40b23ce4SThomas Cort /*
265*40b23ce4SThomas Cort * Add an entry to a pattern list
266*40b23ce4SThomas Cort */
267*40b23ce4SThomas Cort static void
add_pattern(struct pattern_list * list,const char * pattern)268*40b23ce4SThomas Cort add_pattern(struct pattern_list *list, const char *pattern)
269*40b23ce4SThomas Cort {
270*40b23ce4SThomas Cort struct pattern *entry;
271*40b23ce4SThomas Cort size_t len;
272*40b23ce4SThomas Cort
273*40b23ce4SThomas Cort debug("adding pattern '%s'\n", pattern);
274*40b23ce4SThomas Cort len = strlen(pattern);
275*40b23ce4SThomas Cort if ((entry = malloc(sizeof *entry + len + 1)) == NULL) {
276*40b23ce4SThomas Cort errno = ENOMEM;
277*40b23ce4SThomas Cort error("malloc()");
278*40b23ce4SThomas Cort }
279*40b23ce4SThomas Cort memcpy(entry->pattern, pattern, len + 1);
280*40b23ce4SThomas Cort STAILQ_INSERT_TAIL(list, entry, link);
281*40b23ce4SThomas Cort }
282*40b23ce4SThomas Cort
283*40b23ce4SThomas Cort /*
284*40b23ce4SThomas Cort * Match a string against a list of patterns
285*40b23ce4SThomas Cort */
286*40b23ce4SThomas Cort static int
match_pattern(struct pattern_list * list,const char * str)287*40b23ce4SThomas Cort match_pattern(struct pattern_list *list, const char *str)
288*40b23ce4SThomas Cort {
289*40b23ce4SThomas Cort struct pattern *entry;
290*40b23ce4SThomas Cort
291*40b23ce4SThomas Cort STAILQ_FOREACH(entry, list, link) {
292*40b23ce4SThomas Cort if (fnmatch(entry->pattern, str, C_opt ? FNM_CASEFOLD : 0) == 0)
293*40b23ce4SThomas Cort return (1);
294*40b23ce4SThomas Cort }
295*40b23ce4SThomas Cort return (0);
296*40b23ce4SThomas Cort }
297*40b23ce4SThomas Cort
298*40b23ce4SThomas Cort /*
299*40b23ce4SThomas Cort * Verify that a given pathname is in the include list and not in the
300*40b23ce4SThomas Cort * exclude list.
301*40b23ce4SThomas Cort */
302*40b23ce4SThomas Cort static int
accept_pathname(const char * pathname)303*40b23ce4SThomas Cort accept_pathname(const char *pathname)
304*40b23ce4SThomas Cort {
305*40b23ce4SThomas Cort
306*40b23ce4SThomas Cort if (!STAILQ_EMPTY(&include) && !match_pattern(&include, pathname))
307*40b23ce4SThomas Cort return (0);
308*40b23ce4SThomas Cort if (!STAILQ_EMPTY(&exclude) && match_pattern(&exclude, pathname))
309*40b23ce4SThomas Cort return (0);
310*40b23ce4SThomas Cort return (1);
311*40b23ce4SThomas Cort }
312*40b23ce4SThomas Cort
313*40b23ce4SThomas Cort /*
314*40b23ce4SThomas Cort * Create the specified directory with the specified mode, taking certain
315*40b23ce4SThomas Cort * precautions on they way.
316*40b23ce4SThomas Cort */
317*40b23ce4SThomas Cort static void
make_dir(const char * path,int mode)318*40b23ce4SThomas Cort make_dir(const char *path, int mode)
319*40b23ce4SThomas Cort {
320*40b23ce4SThomas Cort struct stat sb;
321*40b23ce4SThomas Cort
322*40b23ce4SThomas Cort if (lstat(path, &sb) == 0) {
323*40b23ce4SThomas Cort if (S_ISDIR(sb.st_mode))
324*40b23ce4SThomas Cort return;
325*40b23ce4SThomas Cort /*
326*40b23ce4SThomas Cort * Normally, we should either ask the user about removing
327*40b23ce4SThomas Cort * the non-directory of the same name as a directory we
328*40b23ce4SThomas Cort * wish to create, or respect the -n or -o command-line
329*40b23ce4SThomas Cort * options. However, this may lead to a later failure or
330*40b23ce4SThomas Cort * even compromise (if this non-directory happens to be a
331*40b23ce4SThomas Cort * symlink to somewhere unsafe), so we don't.
332*40b23ce4SThomas Cort */
333*40b23ce4SThomas Cort
334*40b23ce4SThomas Cort /*
335*40b23ce4SThomas Cort * Don't check unlink() result; failure will cause mkdir()
336*40b23ce4SThomas Cort * to fail later, which we will catch.
337*40b23ce4SThomas Cort */
338*40b23ce4SThomas Cort (void)unlink(path);
339*40b23ce4SThomas Cort }
340*40b23ce4SThomas Cort if (mkdir(path, mode) != 0 && errno != EEXIST)
341*40b23ce4SThomas Cort error("mkdir('%s')", path);
342*40b23ce4SThomas Cort }
343*40b23ce4SThomas Cort
344*40b23ce4SThomas Cort /*
345*40b23ce4SThomas Cort * Ensure that all directories leading up to (but not including) the
346*40b23ce4SThomas Cort * specified path exist.
347*40b23ce4SThomas Cort *
348*40b23ce4SThomas Cort * XXX inefficient + modifies the file in-place
349*40b23ce4SThomas Cort */
350*40b23ce4SThomas Cort static void
make_parent(char * path)351*40b23ce4SThomas Cort make_parent(char *path)
352*40b23ce4SThomas Cort {
353*40b23ce4SThomas Cort struct stat sb;
354*40b23ce4SThomas Cort char *sep;
355*40b23ce4SThomas Cort
356*40b23ce4SThomas Cort sep = strrchr(path, '/');
357*40b23ce4SThomas Cort if (sep == NULL || sep == path)
358*40b23ce4SThomas Cort return;
359*40b23ce4SThomas Cort *sep = '\0';
360*40b23ce4SThomas Cort if (lstat(path, &sb) == 0) {
361*40b23ce4SThomas Cort if (S_ISDIR(sb.st_mode)) {
362*40b23ce4SThomas Cort *sep = '/';
363*40b23ce4SThomas Cort return;
364*40b23ce4SThomas Cort }
365*40b23ce4SThomas Cort unlink(path);
366*40b23ce4SThomas Cort }
367*40b23ce4SThomas Cort make_parent(path);
368*40b23ce4SThomas Cort mkdir(path, 0755);
369*40b23ce4SThomas Cort *sep = '/';
370*40b23ce4SThomas Cort
371*40b23ce4SThomas Cort #if 0
372*40b23ce4SThomas Cort for (sep = path; (sep = strchr(sep, '/')) != NULL; sep++) {
373*40b23ce4SThomas Cort /* root in case of absolute d_arg */
374*40b23ce4SThomas Cort if (sep == path)
375*40b23ce4SThomas Cort continue;
376*40b23ce4SThomas Cort *sep = '\0';
377*40b23ce4SThomas Cort make_dir(path, 0755);
378*40b23ce4SThomas Cort *sep = '/';
379*40b23ce4SThomas Cort }
380*40b23ce4SThomas Cort #endif
381*40b23ce4SThomas Cort }
382*40b23ce4SThomas Cort
383*40b23ce4SThomas Cort /*
384*40b23ce4SThomas Cort * Extract a directory.
385*40b23ce4SThomas Cort */
386*40b23ce4SThomas Cort static void
extract_dir(struct archive * a,struct archive_entry * e,const char * path)387*40b23ce4SThomas Cort extract_dir(struct archive *a, struct archive_entry *e, const char *path)
388*40b23ce4SThomas Cort {
389*40b23ce4SThomas Cort int mode;
390*40b23ce4SThomas Cort
391*40b23ce4SThomas Cort mode = archive_entry_mode(e) & 0777;
392*40b23ce4SThomas Cort if (mode == 0)
393*40b23ce4SThomas Cort mode = 0755;
394*40b23ce4SThomas Cort
395*40b23ce4SThomas Cort /*
396*40b23ce4SThomas Cort * Some zipfiles contain directories with weird permissions such
397*40b23ce4SThomas Cort * as 0644 or 0444. This can cause strange issues such as being
398*40b23ce4SThomas Cort * unable to extract files into the directory we just created, or
399*40b23ce4SThomas Cort * the user being unable to remove the directory later without
400*40b23ce4SThomas Cort * first manually changing its permissions. Therefore, we whack
401*40b23ce4SThomas Cort * the permissions into shape, assuming that the user wants full
402*40b23ce4SThomas Cort * access and that anyone who gets read access also gets execute
403*40b23ce4SThomas Cort * access.
404*40b23ce4SThomas Cort */
405*40b23ce4SThomas Cort mode |= 0700;
406*40b23ce4SThomas Cort if (mode & 0040)
407*40b23ce4SThomas Cort mode |= 0010;
408*40b23ce4SThomas Cort if (mode & 0004)
409*40b23ce4SThomas Cort mode |= 0001;
410*40b23ce4SThomas Cort
411*40b23ce4SThomas Cort info(" creating: %s/\n", path);
412*40b23ce4SThomas Cort make_dir(path, mode);
413*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
414*40b23ce4SThomas Cort }
415*40b23ce4SThomas Cort
416*40b23ce4SThomas Cort static unsigned char buffer[8192];
417*40b23ce4SThomas Cort static char spinner[] = { '|', '/', '-', '\\' };
418*40b23ce4SThomas Cort
419*40b23ce4SThomas Cort static int
handle_existing_file(char ** path)420*40b23ce4SThomas Cort handle_existing_file(char **path)
421*40b23ce4SThomas Cort {
422*40b23ce4SThomas Cort size_t alen;
423*40b23ce4SThomas Cort ssize_t len;
424*40b23ce4SThomas Cort char buf[4];
425*40b23ce4SThomas Cort
426*40b23ce4SThomas Cort for (;;) {
427*40b23ce4SThomas Cort fprintf(stderr,
428*40b23ce4SThomas Cort "replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ",
429*40b23ce4SThomas Cort *path);
430*40b23ce4SThomas Cort fgets(buf, 4, stdin);
431*40b23ce4SThomas Cort switch (*buf) {
432*40b23ce4SThomas Cort case 'A':
433*40b23ce4SThomas Cort o_opt = 1;
434*40b23ce4SThomas Cort /* FALL THROUGH */
435*40b23ce4SThomas Cort case 'y':
436*40b23ce4SThomas Cort case 'Y':
437*40b23ce4SThomas Cort (void)unlink(*path);
438*40b23ce4SThomas Cort return 1;
439*40b23ce4SThomas Cort case 'N':
440*40b23ce4SThomas Cort n_opt = 1;
441*40b23ce4SThomas Cort /* FALL THROUGH */
442*40b23ce4SThomas Cort case 'n':
443*40b23ce4SThomas Cort return -1;
444*40b23ce4SThomas Cort case 'r':
445*40b23ce4SThomas Cort case 'R':
446*40b23ce4SThomas Cort printf("New name: ");
447*40b23ce4SThomas Cort fflush(stdout);
448*40b23ce4SThomas Cort free(*path);
449*40b23ce4SThomas Cort *path = NULL;
450*40b23ce4SThomas Cort alen = 0;
451*40b23ce4SThomas Cort len = getline(path, &alen, stdin);
452*40b23ce4SThomas Cort if ((*path)[len - 1] == '\n')
453*40b23ce4SThomas Cort (*path)[len - 1] = '\0';
454*40b23ce4SThomas Cort return 0;
455*40b23ce4SThomas Cort default:
456*40b23ce4SThomas Cort break;
457*40b23ce4SThomas Cort }
458*40b23ce4SThomas Cort }
459*40b23ce4SThomas Cort }
460*40b23ce4SThomas Cort
461*40b23ce4SThomas Cort /*
462*40b23ce4SThomas Cort * Detect binary files by a combination of character white list and
463*40b23ce4SThomas Cort * black list. NUL bytes and other control codes without use in text files
464*40b23ce4SThomas Cort * result directly in switching the file to binary mode. Otherwise, at least
465*40b23ce4SThomas Cort * one white-listed byte has to be found.
466*40b23ce4SThomas Cort *
467*40b23ce4SThomas Cort * Black-listed: 0..6, 14..25, 28..31
468*40b23ce4SThomas Cort * White-listed: 9..10, 13, >= 32
469*40b23ce4SThomas Cort *
470*40b23ce4SThomas Cort * See the proginfo/txtvsbin.txt in the zip sources for a detailed discussion.
471*40b23ce4SThomas Cort */
472*40b23ce4SThomas Cort #define BYTE_IS_BINARY(x) ((x) < 32 && (0xf3ffc07fU & (1U << (x))))
473*40b23ce4SThomas Cort #define BYTE_IS_TEXT(x) ((x) >= 32 || (0x00002600U & (1U << (x))))
474*40b23ce4SThomas Cort
475*40b23ce4SThomas Cort static int
check_binary(const unsigned char * buf,size_t len)476*40b23ce4SThomas Cort check_binary(const unsigned char *buf, size_t len)
477*40b23ce4SThomas Cort {
478*40b23ce4SThomas Cort int rv;
479*40b23ce4SThomas Cort for (rv = 1; len--; ++buf) {
480*40b23ce4SThomas Cort if (BYTE_IS_BINARY(*buf))
481*40b23ce4SThomas Cort return 1;
482*40b23ce4SThomas Cort if (BYTE_IS_TEXT(*buf))
483*40b23ce4SThomas Cort rv = 0;
484*40b23ce4SThomas Cort }
485*40b23ce4SThomas Cort
486*40b23ce4SThomas Cort return rv;
487*40b23ce4SThomas Cort }
488*40b23ce4SThomas Cort
489*40b23ce4SThomas Cort /*
490*40b23ce4SThomas Cort * Extract a regular file.
491*40b23ce4SThomas Cort */
492*40b23ce4SThomas Cort static void
extract_file(struct archive * a,struct archive_entry * e,char ** path)493*40b23ce4SThomas Cort extract_file(struct archive *a, struct archive_entry *e, char **path)
494*40b23ce4SThomas Cort {
495*40b23ce4SThomas Cort int mode;
496*40b23ce4SThomas Cort time_t mtime;
497*40b23ce4SThomas Cort struct stat sb;
498*40b23ce4SThomas Cort struct timeval tv[2];
499*40b23ce4SThomas Cort int cr, fd, text, warn, check;
500*40b23ce4SThomas Cort ssize_t len;
501*40b23ce4SThomas Cort unsigned char *p, *q, *end;
502*40b23ce4SThomas Cort
503*40b23ce4SThomas Cort mode = archive_entry_mode(e) & 0777;
504*40b23ce4SThomas Cort if (mode == 0)
505*40b23ce4SThomas Cort mode = 0644;
506*40b23ce4SThomas Cort mtime = archive_entry_mtime(e);
507*40b23ce4SThomas Cort
508*40b23ce4SThomas Cort /* look for existing file of same name */
509*40b23ce4SThomas Cort recheck:
510*40b23ce4SThomas Cort if (lstat(*path, &sb) == 0) {
511*40b23ce4SThomas Cort if (u_opt || f_opt) {
512*40b23ce4SThomas Cort /* check if up-to-date */
513*40b23ce4SThomas Cort if (S_ISREG(sb.st_mode) && sb.st_mtime >= mtime)
514*40b23ce4SThomas Cort return;
515*40b23ce4SThomas Cort (void)unlink(*path);
516*40b23ce4SThomas Cort } else if (o_opt) {
517*40b23ce4SThomas Cort /* overwrite */
518*40b23ce4SThomas Cort (void)unlink(*path);
519*40b23ce4SThomas Cort } else if (n_opt) {
520*40b23ce4SThomas Cort /* do not overwrite */
521*40b23ce4SThomas Cort return;
522*40b23ce4SThomas Cort } else {
523*40b23ce4SThomas Cort check = handle_existing_file(path);
524*40b23ce4SThomas Cort if (check == 0)
525*40b23ce4SThomas Cort goto recheck;
526*40b23ce4SThomas Cort if (check == -1)
527*40b23ce4SThomas Cort return; /* do not overwrite */
528*40b23ce4SThomas Cort }
529*40b23ce4SThomas Cort } else {
530*40b23ce4SThomas Cort if (f_opt)
531*40b23ce4SThomas Cort return;
532*40b23ce4SThomas Cort }
533*40b23ce4SThomas Cort
534*40b23ce4SThomas Cort if ((fd = open(*path, O_RDWR|O_CREAT|O_TRUNC, mode)) < 0)
535*40b23ce4SThomas Cort error("open('%s')", *path);
536*40b23ce4SThomas Cort
537*40b23ce4SThomas Cort /* loop over file contents and write to disk */
538*40b23ce4SThomas Cort info(" extracting: %s", *path);
539*40b23ce4SThomas Cort text = a_opt;
540*40b23ce4SThomas Cort warn = 0;
541*40b23ce4SThomas Cort cr = 0;
542*40b23ce4SThomas Cort for (int n = 0; ; n++) {
543*40b23ce4SThomas Cort if (tty && (n % 4) == 0)
544*40b23ce4SThomas Cort info(" %c\b\b", spinner[(n / 4) % sizeof spinner]);
545*40b23ce4SThomas Cort
546*40b23ce4SThomas Cort len = archive_read_data(a, buffer, sizeof buffer);
547*40b23ce4SThomas Cort
548*40b23ce4SThomas Cort if (len < 0)
549*40b23ce4SThomas Cort ac(len);
550*40b23ce4SThomas Cort
551*40b23ce4SThomas Cort /* left over CR from previous buffer */
552*40b23ce4SThomas Cort if (a_opt && cr) {
553*40b23ce4SThomas Cort if (len == 0 || buffer[0] != '\n')
554*40b23ce4SThomas Cort if (write(fd, "\r", 1) != 1)
555*40b23ce4SThomas Cort error("write('%s')", *path);
556*40b23ce4SThomas Cort cr = 0;
557*40b23ce4SThomas Cort }
558*40b23ce4SThomas Cort
559*40b23ce4SThomas Cort /* EOF */
560*40b23ce4SThomas Cort if (len == 0)
561*40b23ce4SThomas Cort break;
562*40b23ce4SThomas Cort end = buffer + len;
563*40b23ce4SThomas Cort
564*40b23ce4SThomas Cort /*
565*40b23ce4SThomas Cort * Detect whether this is a text file. The correct way to
566*40b23ce4SThomas Cort * do this is to check the least significant bit of the
567*40b23ce4SThomas Cort * "internal file attributes" field of the corresponding
568*40b23ce4SThomas Cort * file header in the central directory, but libarchive
569*40b23ce4SThomas Cort * does not read the central directory, so we have to
570*40b23ce4SThomas Cort * guess by looking for non-ASCII characters in the
571*40b23ce4SThomas Cort * buffer. Hopefully we won't guess wrong. If we do
572*40b23ce4SThomas Cort * guess wrong, we print a warning message later.
573*40b23ce4SThomas Cort */
574*40b23ce4SThomas Cort if (a_opt && n == 0) {
575*40b23ce4SThomas Cort if (check_binary(buffer, len))
576*40b23ce4SThomas Cort text = 0;
577*40b23ce4SThomas Cort }
578*40b23ce4SThomas Cort
579*40b23ce4SThomas Cort /* simple case */
580*40b23ce4SThomas Cort if (!a_opt || !text) {
581*40b23ce4SThomas Cort if (write(fd, buffer, len) != len)
582*40b23ce4SThomas Cort error("write('%s')", *path);
583*40b23ce4SThomas Cort continue;
584*40b23ce4SThomas Cort }
585*40b23ce4SThomas Cort
586*40b23ce4SThomas Cort /* hard case: convert \r\n to \n (sigh...) */
587*40b23ce4SThomas Cort for (p = buffer; p < end; p = q + 1) {
588*40b23ce4SThomas Cort for (q = p; q < end; q++) {
589*40b23ce4SThomas Cort if (!warn && BYTE_IS_BINARY(*q)) {
590*40b23ce4SThomas Cort warningx("%s may be corrupted due"
591*40b23ce4SThomas Cort " to weak text file detection"
592*40b23ce4SThomas Cort " heuristic", *path);
593*40b23ce4SThomas Cort warn = 1;
594*40b23ce4SThomas Cort }
595*40b23ce4SThomas Cort if (q[0] != '\r')
596*40b23ce4SThomas Cort continue;
597*40b23ce4SThomas Cort if (&q[1] == end) {
598*40b23ce4SThomas Cort cr = 1;
599*40b23ce4SThomas Cort break;
600*40b23ce4SThomas Cort }
601*40b23ce4SThomas Cort if (q[1] == '\n')
602*40b23ce4SThomas Cort break;
603*40b23ce4SThomas Cort }
604*40b23ce4SThomas Cort if (write(fd, p, q - p) != q - p)
605*40b23ce4SThomas Cort error("write('%s')", *path);
606*40b23ce4SThomas Cort }
607*40b23ce4SThomas Cort }
608*40b23ce4SThomas Cort if (tty)
609*40b23ce4SThomas Cort info(" \b\b");
610*40b23ce4SThomas Cort if (text)
611*40b23ce4SThomas Cort info(" (text)");
612*40b23ce4SThomas Cort info("\n");
613*40b23ce4SThomas Cort
614*40b23ce4SThomas Cort /* set access and modification time */
615*40b23ce4SThomas Cort tv[0].tv_sec = now;
616*40b23ce4SThomas Cort tv[0].tv_usec = 0;
617*40b23ce4SThomas Cort tv[1].tv_sec = mtime;
618*40b23ce4SThomas Cort tv[1].tv_usec = 0;
619*40b23ce4SThomas Cort if (futimes(fd, tv) != 0)
620*40b23ce4SThomas Cort error("utimes('%s')", *path);
621*40b23ce4SThomas Cort if (close(fd) != 0)
622*40b23ce4SThomas Cort error("close('%s')", *path);
623*40b23ce4SThomas Cort }
624*40b23ce4SThomas Cort
625*40b23ce4SThomas Cort /*
626*40b23ce4SThomas Cort * Extract a zipfile entry: first perform some sanity checks to ensure
627*40b23ce4SThomas Cort * that it is either a directory or a regular file and that the path is
628*40b23ce4SThomas Cort * not absolute and does not try to break out of the current directory;
629*40b23ce4SThomas Cort * then call either extract_dir() or extract_file() as appropriate.
630*40b23ce4SThomas Cort *
631*40b23ce4SThomas Cort * This is complicated a bit by the various ways in which we need to
632*40b23ce4SThomas Cort * manipulate the path name. Case conversion (if requested by the -L
633*40b23ce4SThomas Cort * option) happens first, but the include / exclude patterns are applied
634*40b23ce4SThomas Cort * to the full converted path name, before the directory part of the path
635*40b23ce4SThomas Cort * is removed in accordance with the -j option. Sanity checks are
636*40b23ce4SThomas Cort * intentionally done earlier than they need to be, so the user will get a
637*40b23ce4SThomas Cort * warning about insecure paths even for files or directories which
638*40b23ce4SThomas Cort * wouldn't be extracted anyway.
639*40b23ce4SThomas Cort */
640*40b23ce4SThomas Cort static void
extract(struct archive * a,struct archive_entry * e)641*40b23ce4SThomas Cort extract(struct archive *a, struct archive_entry *e)
642*40b23ce4SThomas Cort {
643*40b23ce4SThomas Cort char *pathname, *realpathname;
644*40b23ce4SThomas Cort mode_t filetype;
645*40b23ce4SThomas Cort char *p, *q;
646*40b23ce4SThomas Cort
647*40b23ce4SThomas Cort pathname = pathdup(archive_entry_pathname(e));
648*40b23ce4SThomas Cort filetype = archive_entry_filetype(e);
649*40b23ce4SThomas Cort
650*40b23ce4SThomas Cort /* sanity checks */
651*40b23ce4SThomas Cort if (pathname[0] == '/' ||
652*40b23ce4SThomas Cort strncmp(pathname, "../", 3) == 0 ||
653*40b23ce4SThomas Cort strstr(pathname, "/../") != NULL) {
654*40b23ce4SThomas Cort warningx("skipping insecure entry '%s'", pathname);
655*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
656*40b23ce4SThomas Cort free(pathname);
657*40b23ce4SThomas Cort return;
658*40b23ce4SThomas Cort }
659*40b23ce4SThomas Cort
660*40b23ce4SThomas Cort /* I don't think this can happen in a zipfile.. */
661*40b23ce4SThomas Cort if (!S_ISDIR(filetype) && !S_ISREG(filetype)) {
662*40b23ce4SThomas Cort warningx("skipping non-regular entry '%s'", pathname);
663*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
664*40b23ce4SThomas Cort free(pathname);
665*40b23ce4SThomas Cort return;
666*40b23ce4SThomas Cort }
667*40b23ce4SThomas Cort
668*40b23ce4SThomas Cort /* skip directories in -j case */
669*40b23ce4SThomas Cort if (S_ISDIR(filetype) && j_opt) {
670*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
671*40b23ce4SThomas Cort free(pathname);
672*40b23ce4SThomas Cort return;
673*40b23ce4SThomas Cort }
674*40b23ce4SThomas Cort
675*40b23ce4SThomas Cort /* apply include / exclude patterns */
676*40b23ce4SThomas Cort if (!accept_pathname(pathname)) {
677*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
678*40b23ce4SThomas Cort free(pathname);
679*40b23ce4SThomas Cort return;
680*40b23ce4SThomas Cort }
681*40b23ce4SThomas Cort
682*40b23ce4SThomas Cort /* apply -j and -d */
683*40b23ce4SThomas Cort if (j_opt) {
684*40b23ce4SThomas Cort for (p = q = pathname; *p; ++p)
685*40b23ce4SThomas Cort if (*p == '/')
686*40b23ce4SThomas Cort q = p + 1;
687*40b23ce4SThomas Cort realpathname = pathcat(d_arg, q);
688*40b23ce4SThomas Cort } else {
689*40b23ce4SThomas Cort realpathname = pathcat(d_arg, pathname);
690*40b23ce4SThomas Cort }
691*40b23ce4SThomas Cort
692*40b23ce4SThomas Cort /* ensure that parent directory exists */
693*40b23ce4SThomas Cort make_parent(realpathname);
694*40b23ce4SThomas Cort
695*40b23ce4SThomas Cort if (S_ISDIR(filetype))
696*40b23ce4SThomas Cort extract_dir(a, e, realpathname);
697*40b23ce4SThomas Cort else
698*40b23ce4SThomas Cort extract_file(a, e, &realpathname);
699*40b23ce4SThomas Cort
700*40b23ce4SThomas Cort free(realpathname);
701*40b23ce4SThomas Cort free(pathname);
702*40b23ce4SThomas Cort }
703*40b23ce4SThomas Cort
704*40b23ce4SThomas Cort static void
extract_stdout(struct archive * a,struct archive_entry * e)705*40b23ce4SThomas Cort extract_stdout(struct archive *a, struct archive_entry *e)
706*40b23ce4SThomas Cort {
707*40b23ce4SThomas Cort char *pathname;
708*40b23ce4SThomas Cort mode_t filetype;
709*40b23ce4SThomas Cort int cr, text, warn;
710*40b23ce4SThomas Cort ssize_t len;
711*40b23ce4SThomas Cort unsigned char *p, *q, *end;
712*40b23ce4SThomas Cort
713*40b23ce4SThomas Cort pathname = pathdup(archive_entry_pathname(e));
714*40b23ce4SThomas Cort filetype = archive_entry_filetype(e);
715*40b23ce4SThomas Cort
716*40b23ce4SThomas Cort /* I don't think this can happen in a zipfile.. */
717*40b23ce4SThomas Cort if (!S_ISDIR(filetype) && !S_ISREG(filetype)) {
718*40b23ce4SThomas Cort warningx("skipping non-regular entry '%s'", pathname);
719*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
720*40b23ce4SThomas Cort free(pathname);
721*40b23ce4SThomas Cort return;
722*40b23ce4SThomas Cort }
723*40b23ce4SThomas Cort
724*40b23ce4SThomas Cort /* skip directories in -j case */
725*40b23ce4SThomas Cort if (S_ISDIR(filetype)) {
726*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
727*40b23ce4SThomas Cort free(pathname);
728*40b23ce4SThomas Cort return;
729*40b23ce4SThomas Cort }
730*40b23ce4SThomas Cort
731*40b23ce4SThomas Cort /* apply include / exclude patterns */
732*40b23ce4SThomas Cort if (!accept_pathname(pathname)) {
733*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
734*40b23ce4SThomas Cort free(pathname);
735*40b23ce4SThomas Cort return;
736*40b23ce4SThomas Cort }
737*40b23ce4SThomas Cort
738*40b23ce4SThomas Cort if (c_opt)
739*40b23ce4SThomas Cort info("x %s\n", pathname);
740*40b23ce4SThomas Cort
741*40b23ce4SThomas Cort text = a_opt;
742*40b23ce4SThomas Cort warn = 0;
743*40b23ce4SThomas Cort cr = 0;
744*40b23ce4SThomas Cort for (int n = 0; ; n++) {
745*40b23ce4SThomas Cort len = archive_read_data(a, buffer, sizeof buffer);
746*40b23ce4SThomas Cort
747*40b23ce4SThomas Cort if (len < 0)
748*40b23ce4SThomas Cort ac(len);
749*40b23ce4SThomas Cort
750*40b23ce4SThomas Cort /* left over CR from previous buffer */
751*40b23ce4SThomas Cort if (a_opt && cr) {
752*40b23ce4SThomas Cort if (len == 0 || buffer[0] != '\n') {
753*40b23ce4SThomas Cort if (fwrite("\r", 1, 1, stderr) != 1)
754*40b23ce4SThomas Cort error("write('%s')", pathname);
755*40b23ce4SThomas Cort }
756*40b23ce4SThomas Cort cr = 0;
757*40b23ce4SThomas Cort }
758*40b23ce4SThomas Cort
759*40b23ce4SThomas Cort /* EOF */
760*40b23ce4SThomas Cort if (len == 0)
761*40b23ce4SThomas Cort break;
762*40b23ce4SThomas Cort end = buffer + len;
763*40b23ce4SThomas Cort
764*40b23ce4SThomas Cort /*
765*40b23ce4SThomas Cort * Detect whether this is a text file. The correct way to
766*40b23ce4SThomas Cort * do this is to check the least significant bit of the
767*40b23ce4SThomas Cort * "internal file attributes" field of the corresponding
768*40b23ce4SThomas Cort * file header in the central directory, but libarchive
769*40b23ce4SThomas Cort * does not read the central directory, so we have to
770*40b23ce4SThomas Cort * guess by looking for non-ASCII characters in the
771*40b23ce4SThomas Cort * buffer. Hopefully we won't guess wrong. If we do
772*40b23ce4SThomas Cort * guess wrong, we print a warning message later.
773*40b23ce4SThomas Cort */
774*40b23ce4SThomas Cort if (a_opt && n == 0) {
775*40b23ce4SThomas Cort for (p = buffer; p < end; ++p) {
776*40b23ce4SThomas Cort if (!isascii((unsigned char)*p)) {
777*40b23ce4SThomas Cort text = 0;
778*40b23ce4SThomas Cort break;
779*40b23ce4SThomas Cort }
780*40b23ce4SThomas Cort }
781*40b23ce4SThomas Cort }
782*40b23ce4SThomas Cort
783*40b23ce4SThomas Cort /* simple case */
784*40b23ce4SThomas Cort if (!a_opt || !text) {
785*40b23ce4SThomas Cort if (fwrite(buffer, 1, len, stdout) != (size_t)len)
786*40b23ce4SThomas Cort error("write('%s')", pathname);
787*40b23ce4SThomas Cort continue;
788*40b23ce4SThomas Cort }
789*40b23ce4SThomas Cort
790*40b23ce4SThomas Cort /* hard case: convert \r\n to \n (sigh...) */
791*40b23ce4SThomas Cort for (p = buffer; p < end; p = q + 1) {
792*40b23ce4SThomas Cort for (q = p; q < end; q++) {
793*40b23ce4SThomas Cort if (!warn && !isascii(*q)) {
794*40b23ce4SThomas Cort warningx("%s may be corrupted due"
795*40b23ce4SThomas Cort " to weak text file detection"
796*40b23ce4SThomas Cort " heuristic", pathname);
797*40b23ce4SThomas Cort warn = 1;
798*40b23ce4SThomas Cort }
799*40b23ce4SThomas Cort if (q[0] != '\r')
800*40b23ce4SThomas Cort continue;
801*40b23ce4SThomas Cort if (&q[1] == end) {
802*40b23ce4SThomas Cort cr = 1;
803*40b23ce4SThomas Cort break;
804*40b23ce4SThomas Cort }
805*40b23ce4SThomas Cort if (q[1] == '\n')
806*40b23ce4SThomas Cort break;
807*40b23ce4SThomas Cort }
808*40b23ce4SThomas Cort if (fwrite(p, 1, q - p, stdout) != (size_t)(q - p))
809*40b23ce4SThomas Cort error("write('%s')", pathname);
810*40b23ce4SThomas Cort }
811*40b23ce4SThomas Cort }
812*40b23ce4SThomas Cort
813*40b23ce4SThomas Cort free(pathname);
814*40b23ce4SThomas Cort }
815*40b23ce4SThomas Cort
816*40b23ce4SThomas Cort /*
817*40b23ce4SThomas Cort * Print the name of an entry to stdout.
818*40b23ce4SThomas Cort */
819*40b23ce4SThomas Cort static void
list(struct archive * a,struct archive_entry * e)820*40b23ce4SThomas Cort list(struct archive *a, struct archive_entry *e)
821*40b23ce4SThomas Cort {
822*40b23ce4SThomas Cort char buf[20];
823*40b23ce4SThomas Cort time_t mtime;
824*40b23ce4SThomas Cort struct tm *tm;
825*40b23ce4SThomas Cort
826*40b23ce4SThomas Cort mtime = archive_entry_mtime(e);
827*40b23ce4SThomas Cort tm = localtime(&mtime);
828*40b23ce4SThomas Cort if (*y_str)
829*40b23ce4SThomas Cort strftime(buf, sizeof(buf), "%m-%d-%G %R", tm);
830*40b23ce4SThomas Cort else
831*40b23ce4SThomas Cort strftime(buf, sizeof(buf), "%m-%d-%g %R", tm);
832*40b23ce4SThomas Cort
833*40b23ce4SThomas Cort if (v_opt == 1) {
834*40b23ce4SThomas Cort printf(" %8ju %s %s\n",
835*40b23ce4SThomas Cort (uintmax_t)archive_entry_size(e),
836*40b23ce4SThomas Cort buf, archive_entry_pathname(e));
837*40b23ce4SThomas Cort } else if (v_opt == 2) {
838*40b23ce4SThomas Cort printf("%8ju Stored %7ju 0%% %s %08x %s\n",
839*40b23ce4SThomas Cort (uintmax_t)archive_entry_size(e),
840*40b23ce4SThomas Cort (uintmax_t)archive_entry_size(e),
841*40b23ce4SThomas Cort buf,
842*40b23ce4SThomas Cort 0U,
843*40b23ce4SThomas Cort archive_entry_pathname(e));
844*40b23ce4SThomas Cort }
845*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
846*40b23ce4SThomas Cort }
847*40b23ce4SThomas Cort
848*40b23ce4SThomas Cort /*
849*40b23ce4SThomas Cort * Extract to memory to check CRC
850*40b23ce4SThomas Cort */
851*40b23ce4SThomas Cort static int
test(struct archive * a,struct archive_entry * e)852*40b23ce4SThomas Cort test(struct archive *a, struct archive_entry *e)
853*40b23ce4SThomas Cort {
854*40b23ce4SThomas Cort ssize_t len;
855*40b23ce4SThomas Cort int error_count;
856*40b23ce4SThomas Cort
857*40b23ce4SThomas Cort error_count = 0;
858*40b23ce4SThomas Cort if (S_ISDIR(archive_entry_filetype(e)))
859*40b23ce4SThomas Cort return 0;
860*40b23ce4SThomas Cort
861*40b23ce4SThomas Cort info(" testing: %s\t", archive_entry_pathname(e));
862*40b23ce4SThomas Cort while ((len = archive_read_data(a, buffer, sizeof buffer)) > 0)
863*40b23ce4SThomas Cort /* nothing */;
864*40b23ce4SThomas Cort if (len < 0) {
865*40b23ce4SThomas Cort info(" %s\n", archive_error_string(a));
866*40b23ce4SThomas Cort ++error_count;
867*40b23ce4SThomas Cort } else {
868*40b23ce4SThomas Cort info(" OK\n");
869*40b23ce4SThomas Cort }
870*40b23ce4SThomas Cort
871*40b23ce4SThomas Cort /* shouldn't be necessary, but it doesn't hurt */
872*40b23ce4SThomas Cort ac(archive_read_data_skip(a));
873*40b23ce4SThomas Cort
874*40b23ce4SThomas Cort return error_count;
875*40b23ce4SThomas Cort }
876*40b23ce4SThomas Cort
877*40b23ce4SThomas Cort
878*40b23ce4SThomas Cort /*
879*40b23ce4SThomas Cort * Main loop: open the zipfile, iterate over its contents and decide what
880*40b23ce4SThomas Cort * to do with each entry.
881*40b23ce4SThomas Cort */
882*40b23ce4SThomas Cort static void
unzip(const char * fn)883*40b23ce4SThomas Cort unzip(const char *fn)
884*40b23ce4SThomas Cort {
885*40b23ce4SThomas Cort struct archive *a;
886*40b23ce4SThomas Cort struct archive_entry *e;
887*40b23ce4SThomas Cort int fd, ret;
888*40b23ce4SThomas Cort uintmax_t total_size, file_count, error_count;
889*40b23ce4SThomas Cort
890*40b23ce4SThomas Cort if ((fd = open(fn, O_RDONLY)) < 0)
891*40b23ce4SThomas Cort error("%s", fn);
892*40b23ce4SThomas Cort
893*40b23ce4SThomas Cort a = archive_read_new();
894*40b23ce4SThomas Cort ac(archive_read_support_format_zip(a));
895*40b23ce4SThomas Cort ac(archive_read_open_fd(a, fd, 8192));
896*40b23ce4SThomas Cort
897*40b23ce4SThomas Cort if (!q_opt && !p_opt)
898*40b23ce4SThomas Cort printf("Archive: %s\n", fn);
899*40b23ce4SThomas Cort
900*40b23ce4SThomas Cort if (v_opt == 1) {
901*40b23ce4SThomas Cort printf(" Length %sDate Time Name\n", y_str);
902*40b23ce4SThomas Cort printf(" -------- %s---- ---- ----\n", y_str);
903*40b23ce4SThomas Cort } else if (v_opt == 2) {
904*40b23ce4SThomas Cort printf(" Length Method Size Ratio %sDate Time CRC-32 Name\n", y_str);
905*40b23ce4SThomas Cort printf("-------- ------ ------- ----- %s---- ---- ------ ----\n", y_str);
906*40b23ce4SThomas Cort }
907*40b23ce4SThomas Cort
908*40b23ce4SThomas Cort total_size = 0;
909*40b23ce4SThomas Cort file_count = 0;
910*40b23ce4SThomas Cort error_count = 0;
911*40b23ce4SThomas Cort for (;;) {
912*40b23ce4SThomas Cort ret = archive_read_next_header(a, &e);
913*40b23ce4SThomas Cort if (ret == ARCHIVE_EOF)
914*40b23ce4SThomas Cort break;
915*40b23ce4SThomas Cort ac(ret);
916*40b23ce4SThomas Cort if (t_opt)
917*40b23ce4SThomas Cort error_count += test(a, e);
918*40b23ce4SThomas Cort else if (v_opt)
919*40b23ce4SThomas Cort list(a, e);
920*40b23ce4SThomas Cort else if (p_opt || c_opt)
921*40b23ce4SThomas Cort extract_stdout(a, e);
922*40b23ce4SThomas Cort else
923*40b23ce4SThomas Cort extract(a, e);
924*40b23ce4SThomas Cort
925*40b23ce4SThomas Cort total_size += archive_entry_size(e);
926*40b23ce4SThomas Cort ++file_count;
927*40b23ce4SThomas Cort }
928*40b23ce4SThomas Cort
929*40b23ce4SThomas Cort if (v_opt == 1) {
930*40b23ce4SThomas Cort printf(" -------- %s-------\n", y_str);
931*40b23ce4SThomas Cort printf(" %8ju %s%ju file%s\n",
932*40b23ce4SThomas Cort total_size, y_str, file_count, file_count != 1 ? "s" : "");
933*40b23ce4SThomas Cort } else if (v_opt == 2) {
934*40b23ce4SThomas Cort printf("-------- ------- --- %s-------\n", y_str);
935*40b23ce4SThomas Cort printf("%8ju %7ju 0%% %s%ju file%s\n",
936*40b23ce4SThomas Cort total_size, total_size, y_str, file_count,
937*40b23ce4SThomas Cort file_count != 1 ? "s" : "");
938*40b23ce4SThomas Cort }
939*40b23ce4SThomas Cort
940*40b23ce4SThomas Cort ac(archive_read_close(a));
941*40b23ce4SThomas Cort (void)archive_read_finish(a);
942*40b23ce4SThomas Cort
943*40b23ce4SThomas Cort if (close(fd) != 0)
944*40b23ce4SThomas Cort error("%s", fn);
945*40b23ce4SThomas Cort
946*40b23ce4SThomas Cort if (t_opt) {
947*40b23ce4SThomas Cort if (error_count > 0) {
948*40b23ce4SThomas Cort errorx("%ju checksum error(s) found.", error_count);
949*40b23ce4SThomas Cort }
950*40b23ce4SThomas Cort else {
951*40b23ce4SThomas Cort printf("No errors detected in compressed data of %s.\n",
952*40b23ce4SThomas Cort fn);
953*40b23ce4SThomas Cort }
954*40b23ce4SThomas Cort }
955*40b23ce4SThomas Cort }
956*40b23ce4SThomas Cort
957*40b23ce4SThomas Cort static void __dead
usage(void)958*40b23ce4SThomas Cort usage(void)
959*40b23ce4SThomas Cort {
960*40b23ce4SThomas Cort
961*40b23ce4SThomas Cort fprintf(stderr, "Usage: %s [-aCcfjLlnopqtuvy] [-d dir] [-x pattern] "
962*40b23ce4SThomas Cort "zipfile\n", getprogname());
963*40b23ce4SThomas Cort exit(1);
964*40b23ce4SThomas Cort }
965*40b23ce4SThomas Cort
966*40b23ce4SThomas Cort static int
getopts(int argc,char * argv[])967*40b23ce4SThomas Cort getopts(int argc, char *argv[])
968*40b23ce4SThomas Cort {
969*40b23ce4SThomas Cort int opt;
970*40b23ce4SThomas Cort
971*40b23ce4SThomas Cort optreset = optind = 1;
972*40b23ce4SThomas Cort while ((opt = getopt(argc, argv, "aCcd:fjLlnopqtuvyx:")) != -1)
973*40b23ce4SThomas Cort switch (opt) {
974*40b23ce4SThomas Cort case 'a':
975*40b23ce4SThomas Cort a_opt = 1;
976*40b23ce4SThomas Cort break;
977*40b23ce4SThomas Cort case 'C':
978*40b23ce4SThomas Cort C_opt = 1;
979*40b23ce4SThomas Cort break;
980*40b23ce4SThomas Cort case 'c':
981*40b23ce4SThomas Cort c_opt = 1;
982*40b23ce4SThomas Cort break;
983*40b23ce4SThomas Cort case 'd':
984*40b23ce4SThomas Cort d_arg = optarg;
985*40b23ce4SThomas Cort break;
986*40b23ce4SThomas Cort case 'f':
987*40b23ce4SThomas Cort f_opt = 1;
988*40b23ce4SThomas Cort break;
989*40b23ce4SThomas Cort case 'j':
990*40b23ce4SThomas Cort j_opt = 1;
991*40b23ce4SThomas Cort break;
992*40b23ce4SThomas Cort case 'L':
993*40b23ce4SThomas Cort L_opt = 1;
994*40b23ce4SThomas Cort break;
995*40b23ce4SThomas Cort case 'l':
996*40b23ce4SThomas Cort if (v_opt == 0)
997*40b23ce4SThomas Cort v_opt = 1;
998*40b23ce4SThomas Cort break;
999*40b23ce4SThomas Cort case 'n':
1000*40b23ce4SThomas Cort n_opt = 1;
1001*40b23ce4SThomas Cort break;
1002*40b23ce4SThomas Cort case 'o':
1003*40b23ce4SThomas Cort o_opt = 1;
1004*40b23ce4SThomas Cort q_opt = 1;
1005*40b23ce4SThomas Cort break;
1006*40b23ce4SThomas Cort case 'p':
1007*40b23ce4SThomas Cort p_opt = 1;
1008*40b23ce4SThomas Cort break;
1009*40b23ce4SThomas Cort case 'q':
1010*40b23ce4SThomas Cort q_opt = 1;
1011*40b23ce4SThomas Cort break;
1012*40b23ce4SThomas Cort case 't':
1013*40b23ce4SThomas Cort t_opt = 1;
1014*40b23ce4SThomas Cort break;
1015*40b23ce4SThomas Cort case 'u':
1016*40b23ce4SThomas Cort u_opt = 1;
1017*40b23ce4SThomas Cort break;
1018*40b23ce4SThomas Cort case 'v':
1019*40b23ce4SThomas Cort v_opt = 2;
1020*40b23ce4SThomas Cort break;
1021*40b23ce4SThomas Cort case 'x':
1022*40b23ce4SThomas Cort add_pattern(&exclude, optarg);
1023*40b23ce4SThomas Cort break;
1024*40b23ce4SThomas Cort case 'y':
1025*40b23ce4SThomas Cort y_str = " ";
1026*40b23ce4SThomas Cort break;
1027*40b23ce4SThomas Cort default:
1028*40b23ce4SThomas Cort usage();
1029*40b23ce4SThomas Cort }
1030*40b23ce4SThomas Cort
1031*40b23ce4SThomas Cort return (optind);
1032*40b23ce4SThomas Cort }
1033*40b23ce4SThomas Cort
1034*40b23ce4SThomas Cort int
main(int argc,char * argv[])1035*40b23ce4SThomas Cort main(int argc, char *argv[])
1036*40b23ce4SThomas Cort {
1037*40b23ce4SThomas Cort const char *zipfile;
1038*40b23ce4SThomas Cort int nopts;
1039*40b23ce4SThomas Cort
1040*40b23ce4SThomas Cort if (isatty(STDOUT_FILENO))
1041*40b23ce4SThomas Cort tty = 1;
1042*40b23ce4SThomas Cort
1043*40b23ce4SThomas Cort if (getenv("UNZIP_DEBUG") != NULL)
1044*40b23ce4SThomas Cort unzip_debug = 1;
1045*40b23ce4SThomas Cort for (int i = 0; i < argc; ++i)
1046*40b23ce4SThomas Cort debug("%s%c", argv[i], (i < argc - 1) ? ' ' : '\n');
1047*40b23ce4SThomas Cort
1048*40b23ce4SThomas Cort /*
1049*40b23ce4SThomas Cort * Info-ZIP's unzip(1) expects certain options to come before the
1050*40b23ce4SThomas Cort * zipfile name, and others to come after - though it does not
1051*40b23ce4SThomas Cort * enforce this. For simplicity, we accept *all* options both
1052*40b23ce4SThomas Cort * before and after the zipfile name.
1053*40b23ce4SThomas Cort */
1054*40b23ce4SThomas Cort nopts = getopts(argc, argv);
1055*40b23ce4SThomas Cort
1056*40b23ce4SThomas Cort if (argc <= nopts)
1057*40b23ce4SThomas Cort usage();
1058*40b23ce4SThomas Cort zipfile = argv[nopts++];
1059*40b23ce4SThomas Cort
1060*40b23ce4SThomas Cort while (nopts < argc && *argv[nopts] != '-')
1061*40b23ce4SThomas Cort add_pattern(&include, argv[nopts++]);
1062*40b23ce4SThomas Cort
1063*40b23ce4SThomas Cort nopts--; /* fake argv[0] */
1064*40b23ce4SThomas Cort nopts += getopts(argc - nopts, argv + nopts);
1065*40b23ce4SThomas Cort
1066*40b23ce4SThomas Cort if (n_opt + o_opt + u_opt > 1)
1067*40b23ce4SThomas Cort errorx("-n, -o and -u are contradictory");
1068*40b23ce4SThomas Cort
1069*40b23ce4SThomas Cort time(&now);
1070*40b23ce4SThomas Cort
1071*40b23ce4SThomas Cort unzip(zipfile);
1072*40b23ce4SThomas Cort
1073*40b23ce4SThomas Cort exit(0);
1074*40b23ce4SThomas Cort }
1075