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