blob: eed2256773df3c1aa3163099be7ba1ce8775397a [file] [log] [blame]
Glenn L McGrath87ac7022002-01-02 13:52:26 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini unzip implementation for busybox
4 *
Paul Fox0840b762005-07-20 20:26:49 +00005 * Copyright (C) 2004 by Ed Clark
6 *
7 * Loosely based on original busybox unzip applet by Laurence Anderson.
8 * All options and features should work in this version.
Glenn L McGrath87ac7022002-01-02 13:52:26 +00009 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath87ac7022002-01-02 13:52:26 +000011 */
Glenn L McGrathf34b0e92004-06-06 10:22:43 +000012/* For reference see
Paul Fox0840b762005-07-20 20:26:49 +000013 * http://www.pkware.com/company/standards/appnote/
Glenn L McGrathf34b0e92004-06-06 10:22:43 +000014 * http://www.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip
Denys Vlasenko66620fa2013-11-14 09:53:52 +010015 *
16 * TODO
Paul Fox0840b762005-07-20 20:26:49 +000017 * Zip64 + other methods
Paul Fox0840b762005-07-20 20:26:49 +000018 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000019
Denys Vlasenkof6beef62013-11-14 11:39:00 +010020//config:config UNZIP
21//config: bool "unzip"
22//config: default y
23//config: help
24//config: unzip will list or extract files from a ZIP archive,
25//config: commonly found on DOS/WIN systems. The default behavior
26//config: (with no options) is to extract the archive into the
27//config: current directory. Use the `-d' option to extract to a
28//config: directory of your choice.
29
Denys Vlasenkoac216872013-11-14 11:38:18 +010030//applet:IF_UNZIP(APPLET(unzip, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenko66620fa2013-11-14 09:53:52 +010031//kbuild:lib-$(CONFIG_UNZIP) += unzip.o
32
Pere Orga1f4447b2011-03-27 22:40:30 +020033//usage:#define unzip_trivial_usage
Denys Vlasenkoe3e0d2b2012-06-19 12:46:59 +020034//usage: "[-lnopq] FILE[.zip] [FILE]... [-x FILE...] [-d DIR]"
Pere Orga1f4447b2011-03-27 22:40:30 +020035//usage:#define unzip_full_usage "\n\n"
Denys Vlasenkoe3e0d2b2012-06-19 12:46:59 +020036//usage: "Extract FILEs from ZIP archive\n"
37//usage: "\n -l List contents (with -q for short form)"
Denys Vlasenkoc5b01012012-06-15 16:43:26 +020038//usage: "\n -n Never overwrite files (default: ask)"
Pere Orga1f4447b2011-03-27 22:40:30 +020039//usage: "\n -o Overwrite"
Denys Vlasenkoe3e0d2b2012-06-19 12:46:59 +020040//usage: "\n -p Print to stdout"
Pere Orga1f4447b2011-03-27 22:40:30 +020041//usage: "\n -q Quiet"
Denys Vlasenkoe3e0d2b2012-06-19 12:46:59 +020042//usage: "\n -x FILE Exclude FILEs"
43//usage: "\n -d DIR Extract into DIR"
Pere Orga1f4447b2011-03-27 22:40:30 +020044
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000045#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020046#include "bb_archive.h"
Glenn L McGrath87ac7022002-01-02 13:52:26 +000047
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000048enum {
49#if BB_BIG_ENDIAN
50 ZIP_FILEHEADER_MAGIC = 0x504b0304,
Denys Vlasenko4e8ff732010-05-24 04:33:02 +020051 ZIP_CDF_MAGIC = 0x504b0102, /* central directory's file header */
52 ZIP_CDE_MAGIC = 0x504b0506, /* "end of central directory" record */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000053 ZIP_DD_MAGIC = 0x504b0708,
54#else
55 ZIP_FILEHEADER_MAGIC = 0x04034b50,
Denys Vlasenko4e8ff732010-05-24 04:33:02 +020056 ZIP_CDF_MAGIC = 0x02014b50,
Denis Vlasenko006e8622008-09-21 01:01:46 +000057 ZIP_CDE_MAGIC = 0x06054b50,
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000058 ZIP_DD_MAGIC = 0x08074b50,
59#endif
60};
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000061
Paul Foxcb981632007-11-05 23:09:03 +000062#define ZIP_HEADER_LEN 26
63
Paul Fox0840b762005-07-20 20:26:49 +000064typedef union {
Paul Foxcb981632007-11-05 23:09:03 +000065 uint8_t raw[ZIP_HEADER_LEN];
Paul Fox0840b762005-07-20 20:26:49 +000066 struct {
Denis Vlasenko006e8622008-09-21 01:01:46 +000067 uint16_t version; /* 0-1 */
Denys Vlasenkoe98884b2010-05-24 04:46:18 +020068 uint16_t zip_flags; /* 2-3 */
Denis Vlasenko006e8622008-09-21 01:01:46 +000069 uint16_t method; /* 4-5 */
70 uint16_t modtime; /* 6-7 */
71 uint16_t moddate; /* 8-9 */
72 uint32_t crc32 PACKED; /* 10-13 */
73 uint32_t cmpsize PACKED; /* 14-17 */
74 uint32_t ucmpsize PACKED; /* 18-21 */
75 uint16_t filename_len; /* 22-23 */
76 uint16_t extra_len; /* 24-25 */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000077 } formatted PACKED;
78} zip_header_t; /* PACKED - gcc 4.2.1 doesn't like it (spews warning) */
Paul Fox0840b762005-07-20 20:26:49 +000079
Paul Foxcb981632007-11-05 23:09:03 +000080/* Check the offset of the last element, not the length. This leniency
81 * allows for poor packing, whereby the overall struct may be too long,
82 * even though the elements are all in the right place.
83 */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000084struct BUG_zip_header_must_be_26_bytes {
Paul Foxcb981632007-11-05 23:09:03 +000085 char BUG_zip_header_must_be_26_bytes[
Denis Vlasenko006e8622008-09-21 01:01:46 +000086 offsetof(zip_header_t, formatted.extra_len) + 2
87 == ZIP_HEADER_LEN ? 1 : -1];
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000088};
89
Denis Vlasenko006e8622008-09-21 01:01:46 +000090#define FIX_ENDIANNESS_ZIP(zip_header) do { \
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000091 (zip_header).formatted.version = SWAP_LE16((zip_header).formatted.version ); \
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +000092 (zip_header).formatted.method = SWAP_LE16((zip_header).formatted.method ); \
93 (zip_header).formatted.modtime = SWAP_LE16((zip_header).formatted.modtime ); \
94 (zip_header).formatted.moddate = SWAP_LE16((zip_header).formatted.moddate ); \
95 (zip_header).formatted.crc32 = SWAP_LE32((zip_header).formatted.crc32 ); \
96 (zip_header).formatted.cmpsize = SWAP_LE32((zip_header).formatted.cmpsize ); \
97 (zip_header).formatted.ucmpsize = SWAP_LE32((zip_header).formatted.ucmpsize ); \
98 (zip_header).formatted.filename_len = SWAP_LE16((zip_header).formatted.filename_len); \
99 (zip_header).formatted.extra_len = SWAP_LE16((zip_header).formatted.extra_len ); \
100} while (0)
101
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200102#define CDF_HEADER_LEN 42
Denis Vlasenko006e8622008-09-21 01:01:46 +0000103
104typedef union {
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200105 uint8_t raw[CDF_HEADER_LEN];
Denis Vlasenko006e8622008-09-21 01:01:46 +0000106 struct {
107 /* uint32_t signature; 50 4b 01 02 */
108 uint16_t version_made_by; /* 0-1 */
109 uint16_t version_needed; /* 2-3 */
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200110 uint16_t cdf_flags; /* 4-5 */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000111 uint16_t method; /* 6-7 */
112 uint16_t mtime; /* 8-9 */
113 uint16_t mdate; /* 10-11 */
114 uint32_t crc32; /* 12-15 */
115 uint32_t cmpsize; /* 16-19 */
116 uint32_t ucmpsize; /* 20-23 */
117 uint16_t file_name_length; /* 24-25 */
118 uint16_t extra_field_length; /* 26-27 */
119 uint16_t file_comment_length; /* 28-29 */
120 uint16_t disk_number_start; /* 30-31 */
121 uint16_t internal_file_attributes; /* 32-33 */
122 uint32_t external_file_attributes PACKED; /* 34-37 */
123 uint32_t relative_offset_of_local_header PACKED; /* 38-41 */
124 } formatted PACKED;
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200125} cdf_header_t;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000126
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200127struct BUG_cdf_header_must_be_42_bytes {
128 char BUG_cdf_header_must_be_42_bytes[
129 offsetof(cdf_header_t, formatted.relative_offset_of_local_header) + 4
130 == CDF_HEADER_LEN ? 1 : -1];
Denis Vlasenko006e8622008-09-21 01:01:46 +0000131};
132
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200133#define FIX_ENDIANNESS_CDF(cdf_header) do { \
134 (cdf_header).formatted.crc32 = SWAP_LE32((cdf_header).formatted.crc32 ); \
135 (cdf_header).formatted.cmpsize = SWAP_LE32((cdf_header).formatted.cmpsize ); \
136 (cdf_header).formatted.ucmpsize = SWAP_LE32((cdf_header).formatted.ucmpsize ); \
137 (cdf_header).formatted.file_name_length = SWAP_LE16((cdf_header).formatted.file_name_length); \
138 (cdf_header).formatted.extra_field_length = SWAP_LE16((cdf_header).formatted.extra_field_length); \
139 (cdf_header).formatted.file_comment_length = SWAP_LE16((cdf_header).formatted.file_comment_length); \
140 IF_DESKTOP( \
141 (cdf_header).formatted.version_made_by = SWAP_LE16((cdf_header).formatted.version_made_by); \
142 (cdf_header).formatted.external_file_attributes = SWAP_LE32((cdf_header).formatted.external_file_attributes); \
143 ) \
Denis Vlasenko006e8622008-09-21 01:01:46 +0000144} while (0)
145
146#define CDE_HEADER_LEN 16
147
148typedef union {
149 uint8_t raw[CDE_HEADER_LEN];
150 struct {
151 /* uint32_t signature; 50 4b 05 06 */
152 uint16_t this_disk_no;
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200153 uint16_t disk_with_cdf_no;
154 uint16_t cdf_entries_on_this_disk;
155 uint16_t cdf_entries_total;
156 uint32_t cdf_size;
157 uint32_t cdf_offset;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000158 /* uint16_t file_comment_length; */
159 /* .ZIP file comment (variable size) */
160 } formatted PACKED;
161} cde_header_t;
162
163struct BUG_cde_header_must_be_16_bytes {
164 char BUG_cde_header_must_be_16_bytes[
165 sizeof(cde_header_t) == CDE_HEADER_LEN ? 1 : -1];
166};
167
168#define FIX_ENDIANNESS_CDE(cde_header) do { \
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200169 (cde_header).formatted.cdf_offset = SWAP_LE32((cde_header).formatted.cdf_offset); \
Denis Vlasenko006e8622008-09-21 01:01:46 +0000170} while (0)
171
172enum { zip_fd = 3 };
173
174
175#if ENABLE_DESKTOP
Dan Fandrichb76f18d2010-06-17 21:39:44 -0700176
Denys Vlasenko5e87e8a2013-07-20 15:20:46 +0200177/* Seen in the wild:
178 * Self-extracting PRO2K3XP_32.exe contains 19078464 byte zip archive,
179 * where CDE was nearly 48 kbytes before EOF.
180 * (Surprisingly, it also apparently has *another* CDE structure
181 * closer to the end, with bogus cdf_offset).
182 * To make extraction work, bumped PEEK_FROM_END from 16k to 64k.
183 */
184#define PEEK_FROM_END (64*1024)
Dan Fandrichb76f18d2010-06-17 21:39:44 -0700185
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200186/* This value means that we failed to find CDF */
187#define BAD_CDF_OFFSET ((uint32_t)0xffffffff)
188
Denis Vlasenko006e8622008-09-21 01:01:46 +0000189/* NB: does not preserve file position! */
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200190static uint32_t find_cdf_offset(void)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000191{
Denis Vlasenko006e8622008-09-21 01:01:46 +0000192 cde_header_t cde_header;
193 unsigned char *p;
194 off_t end;
Dan Fandrichb76f18d2010-06-17 21:39:44 -0700195 unsigned char *buf = xzalloc(PEEK_FROM_END);
Denis Vlasenko006e8622008-09-21 01:01:46 +0000196
197 end = xlseek(zip_fd, 0, SEEK_END);
Dan Fandrichb76f18d2010-06-17 21:39:44 -0700198 end -= PEEK_FROM_END;
Denys Vlasenkofc2bb8f2010-05-24 13:07:55 +0200199 if (end < 0)
200 end = 0;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000201 xlseek(zip_fd, end, SEEK_SET);
Dan Fandrichb76f18d2010-06-17 21:39:44 -0700202 full_read(zip_fd, buf, PEEK_FROM_END);
Denis Vlasenko006e8622008-09-21 01:01:46 +0000203
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200204 cde_header.formatted.cdf_offset = BAD_CDF_OFFSET;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000205 p = buf;
Dan Fandrichb76f18d2010-06-17 21:39:44 -0700206 while (p <= buf + PEEK_FROM_END - CDE_HEADER_LEN - 4) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000207 if (*p != 'P') {
208 p++;
209 continue;
210 }
211 if (*++p != 'K')
212 continue;
213 if (*++p != 5)
214 continue;
215 if (*++p != 6)
216 continue;
217 /* we found CDE! */
218 memcpy(cde_header.raw, p + 1, CDE_HEADER_LEN);
219 FIX_ENDIANNESS_CDE(cde_header);
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200220 /*
221 * I've seen .ZIP files with seemingly valid CDEs
222 * where cdf_offset points past EOF - ??
223 * Ignore such CDEs:
224 */
225 if (cde_header.formatted.cdf_offset < end + (p - buf))
226 break;
227 cde_header.formatted.cdf_offset = BAD_CDF_OFFSET;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000228 }
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200229 free(buf);
230 return cde_header.formatted.cdf_offset;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000231};
232
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200233static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr)
Denis Vlasenko006e8622008-09-21 01:01:46 +0000234{
235 off_t org;
236
237 org = xlseek(zip_fd, 0, SEEK_CUR);
238
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200239 if (!cdf_offset)
240 cdf_offset = find_cdf_offset();
Denis Vlasenko006e8622008-09-21 01:01:46 +0000241
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200242 if (cdf_offset != BAD_CDF_OFFSET) {
243 xlseek(zip_fd, cdf_offset + 4, SEEK_SET);
244 xread(zip_fd, cdf_ptr->raw, CDF_HEADER_LEN);
245 FIX_ENDIANNESS_CDF(*cdf_ptr);
246 cdf_offset += 4 + CDF_HEADER_LEN
247 + cdf_ptr->formatted.file_name_length
248 + cdf_ptr->formatted.extra_field_length
249 + cdf_ptr->formatted.file_comment_length;
250 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000251
252 xlseek(zip_fd, org, SEEK_SET);
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200253 return cdf_offset;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000254};
255#endif
256
257static void unzip_skip(off_t skip)
258{
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200259 if (skip != 0)
260 if (lseek(zip_fd, skip, SEEK_CUR) == (off_t)-1)
261 bb_copyfd_exact_size(zip_fd, -1, skip);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000262}
263
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000264static void unzip_create_leading_dirs(const char *fn)
Paul Fox0840b762005-07-20 20:26:49 +0000265{
266 /* Create all leading directories */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000267 char *name = xstrdup(fn);
Paul Fox0840b762005-07-20 20:26:49 +0000268 if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
Denys Vlasenko831756b2011-09-09 17:30:55 +0200269 xfunc_die(); /* bb_make_directory is noisy */
Paul Fox0840b762005-07-20 20:26:49 +0000270 }
271 free(name);
272}
273
Denis Vlasenko006e8622008-09-21 01:01:46 +0000274static void unzip_extract(zip_header_t *zip_header, int dst_fd)
Paul Fox0840b762005-07-20 20:26:49 +0000275{
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000276 if (zip_header->formatted.method == 0) {
Paul Fox0840b762005-07-20 20:26:49 +0000277 /* Method 0 - stored (not compressed) */
Denis Vlasenko7039a662006-10-08 17:54:47 +0000278 off_t size = zip_header->formatted.ucmpsize;
Denis Vlasenko714701c2006-12-22 00:21:07 +0000279 if (size)
Denis Vlasenko006e8622008-09-21 01:01:46 +0000280 bb_copyfd_exact_size(zip_fd, dst_fd, size);
Paul Fox0840b762005-07-20 20:26:49 +0000281 } else {
282 /* Method 8 - inflate */
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100283 transformer_state_t xstate;
284 init_transformer_state(&xstate);
285 xstate.bytes_in = zip_header->formatted.cmpsize;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100286 xstate.src_fd = zip_fd;
287 xstate.dst_fd = dst_fd;
288 if (inflate_unzip(&xstate) < 0)
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000289 bb_error_msg_and_die("inflate error");
Paul Fox0840b762005-07-20 20:26:49 +0000290 /* Validate decompression - crc */
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100291 if (zip_header->formatted.crc32 != (xstate.crc32 ^ 0xffffffffL)) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000292 bb_error_msg_and_die("crc error");
Paul Fox0840b762005-07-20 20:26:49 +0000293 }
294 /* Validate decompression - size */
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100295 if (zip_header->formatted.ucmpsize != xstate.bytes_out) {
Denis Vlasenkofcc56962007-10-19 21:03:09 +0000296 /* Don't die. Who knows, maybe len calculation
297 * was botched somewhere. After all, crc matched! */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000298 bb_error_msg("bad length");
Paul Fox0840b762005-07-20 20:26:49 +0000299 }
300 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000301}
302
Denys Vlasenkobf998072013-01-22 11:16:08 +0100303static void my_fgets80(char *buf80)
304{
305 fflush_all();
306 if (!fgets(buf80, 80, stdin)) {
307 bb_perror_msg_and_die("can't read standard input");
308 }
309}
310
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000311int unzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000312int unzip_main(int argc, char **argv)
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000313{
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000314 enum { O_PROMPT, O_NEVER, O_ALWAYS };
315
Paul Fox0840b762005-07-20 20:26:49 +0000316 zip_header_t zip_header;
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200317 smallint quiet = 0;
318 IF_NOT_DESKTOP(const) smallint verbose = 0;
Paul Fox9382b382007-09-07 20:28:25 +0000319 smallint listing = 0;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000320 smallint overwrite = O_PROMPT;
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200321 smallint x_opt_seen;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000322#if ENABLE_DESKTOP
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200323 uint32_t cdf_offset;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000324#endif
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200325 unsigned long total_usize;
326 unsigned long total_size;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000327 unsigned total_entries;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000328 int dst_fd = -1;
329 char *src_fn = NULL;
330 char *dst_fn = NULL;
Mike Frysinger69024552005-07-30 07:30:26 +0000331 llist_t *zaccept = NULL;
332 llist_t *zreject = NULL;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000333 char *base_dir = NULL;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000334 int i, opt;
Denys Vlasenkobf998072013-01-22 11:16:08 +0100335 char key_buf[80]; /* must match size used by my_fgets80 */
Paul Fox0840b762005-07-20 20:26:49 +0000336 struct stat stat_buf;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000337
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200338/* -q, -l and -v: UnZip 5.52 of 28 February 2005, by Info-ZIP:
339 *
340 * # /usr/bin/unzip -qq -v decompress_unlzma.i.zip
341 * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
342 * # /usr/bin/unzip -q -v decompress_unlzma.i.zip
343 * Length Method Size Ratio Date Time CRC-32 Name
344 * -------- ------ ------- ----- ---- ---- ------ ----
345 * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
346 * -------- ------- --- -------
347 * 204372 35278 83% 1 file
348 * # /usr/bin/unzip -v decompress_unlzma.i.zip
349 * Archive: decompress_unlzma.i.zip
350 * Length Method Size Ratio Date Time CRC-32 Name
351 * -------- ------ ------- ----- ---- ---- ------ ----
352 * 204372 Defl:N 35278 83% 09-06-09 14:23 0d056252 decompress_unlzma.i
353 * -------- ------- --- -------
354 * 204372 35278 83% 1 file
355 * # unzip -v decompress_unlzma.i.zip
356 * Archive: decompress_unlzma.i.zip
357 * Length Date Time Name
358 * -------- ---- ---- ----
359 * 204372 09-06-09 14:23 decompress_unlzma.i
360 * -------- -------
361 * 204372 1 files
362 * # /usr/bin/unzip -l -qq decompress_unlzma.i.zip
363 * 204372 09-06-09 14:23 decompress_unlzma.i
364 * # /usr/bin/unzip -l -q decompress_unlzma.i.zip
365 * Length Date Time Name
366 * -------- ---- ---- ----
367 * 204372 09-06-09 14:23 decompress_unlzma.i
368 * -------- -------
369 * 204372 1 file
370 * # /usr/bin/unzip -l decompress_unlzma.i.zip
371 * Archive: decompress_unlzma.i.zip
372 * Length Date Time Name
373 * -------- ---- ---- ----
374 * 204372 09-06-09 14:23 decompress_unlzma.i
375 * -------- -------
376 * 204372 1 file
377 */
378
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200379 x_opt_seen = 0;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000380 /* '-' makes getopt return 1 for non-options */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200381 while ((opt = getopt(argc, argv, "-d:lnopqxv")) != -1) {
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200382 switch (opt) {
383 case 'd': /* Extract to base directory */
384 base_dir = optarg;
385 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000386
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200387 case 'l': /* List */
388 listing = 1;
389 break;
Paul Fox0840b762005-07-20 20:26:49 +0000390
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200391 case 'n': /* Never overwrite existing files */
392 overwrite = O_NEVER;
393 break;
Paul Fox0840b762005-07-20 20:26:49 +0000394
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200395 case 'o': /* Always overwrite existing files */
396 overwrite = O_ALWAYS;
397 break;
Paul Fox0840b762005-07-20 20:26:49 +0000398
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200399 case 'p': /* Extract files to stdout and fall through to set verbosity */
400 dst_fd = STDOUT_FILENO;
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200401
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200402 case 'q': /* Be quiet */
403 quiet++;
404 break;
Paul Fox0840b762005-07-20 20:26:49 +0000405
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200406 case 'v': /* Verbose list */
407 IF_DESKTOP(verbose++;)
408 listing = 1;
409 break;
410
411 case 'x':
412 x_opt_seen = 1;
413 break;
414
415 case 1:
416 if (!src_fn) {
417 /* The zip file */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000418 /* +5: space for ".zip" and NUL */
419 src_fn = xmalloc(strlen(optarg) + 5);
Denis Vlasenko666c40c2007-03-31 10:17:24 +0000420 strcpy(src_fn, optarg);
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200421 } else if (!x_opt_seen) {
422 /* Include files */
423 llist_add_to(&zaccept, optarg);
424 } else {
425 /* Exclude files */
426 llist_add_to(&zreject, optarg);
Paul Fox0840b762005-07-20 20:26:49 +0000427 }
428 break;
429
Paul Fox0840b762005-07-20 20:26:49 +0000430 default:
431 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000432 }
433 }
434
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200435#ifndef __GLIBC__
436 /*
437 * This code is needed for non-GNU getopt
438 * which doesn't understand "-" in option string.
439 * The -x option won't work properly in this case:
440 * "unzip a.zip q -x w e" will be interpreted as
441 * "unzip a.zip q w e -x" = "unzip a.zip q w e"
442 */
443 argv += optind;
444 if (argv[0]) {
445 /* +5: space for ".zip" and NUL */
446 src_fn = xmalloc(strlen(argv[0]) + 5);
447 strcpy(src_fn, argv[0]);
448 while (*++argv)
449 llist_add_to(&zaccept, *argv);
450 }
451#endif
452
453 if (!src_fn) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000454 bb_show_usage();
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000455 }
456
Paul Fox0840b762005-07-20 20:26:49 +0000457 /* Open input file */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000458 if (LONE_DASH(src_fn)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000459 xdup2(STDIN_FILENO, zip_fd);
Paul Fox0840b762005-07-20 20:26:49 +0000460 /* Cannot use prompt mode since zip data is arriving on STDIN */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000461 if (overwrite == O_PROMPT)
462 overwrite = O_NEVER;
Glenn L McGrath237ae422002-11-03 14:05:15 +0000463 } else {
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200464 static const char extn[][5] = { ".zip", ".ZIP" };
465 char *ext = src_fn + strlen(src_fn);
466 int src_fd;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000467
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200468 i = 0;
469 for (;;) {
Paul Fox0840b762005-07-20 20:26:49 +0000470 src_fd = open(src_fn, O_RDONLY);
Denys Vlasenkoc5b01012012-06-15 16:43:26 +0200471 if (src_fd >= 0)
472 break;
473 if (++i > 2) {
474 *ext = '\0';
475 bb_error_msg_and_die("can't open %s[.zip]", src_fn);
476 }
477 strcpy(ext, extn[i - 1]);
Paul Fox0840b762005-07-20 20:26:49 +0000478 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000479 xmove_fd(src_fd, zip_fd);
Glenn L McGrath237ae422002-11-03 14:05:15 +0000480 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000481
Paul Fox0840b762005-07-20 20:26:49 +0000482 /* Change dir if necessary */
Bernhard Reutner-Fischerd9cf7ac2006-04-12 18:39:58 +0000483 if (base_dir)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000484 xchdir(base_dir);
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000485
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200486 if (quiet <= 1) { /* not -qq */
487 if (quiet == 0)
488 printf("Archive: %s\n", src_fn);
489 if (listing) {
490 puts(verbose ?
491 " Length Method Size Ratio Date Time CRC-32 Name\n"
492 "-------- ------ ------- ----- ---- ---- ------ ----"
493 :
494 " Length Date Time Name\n"
495 " -------- ---- ---- ----"
496 );
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000497 }
498 }
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000499
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200500/* Example of an archive with one 0-byte long file named 'z'
501 * created by Zip 2.31 on Unix:
502 * 0000 [50 4b]03 04 0a 00 00 00 00 00 42 1a b8 3c 00 00 |PK........B..<..|
503 * sig........ vneed flags compr mtime mdate crc32>
504 * 0010 00 00 00 00 00 00 00 00 00 00 01 00 15 00 7a 55 |..............zU|
505 * >..... csize...... usize...... fnlen exlen fn ex>
506 * 0020 54 09 00 03 cc d3 f9 4b cc d3 f9 4b 55 78 04 00 |T......K...KUx..|
507 * >tra_field......................................
508 * 0030 00 00 00 00[50 4b]01 02 17 03 0a 00 00 00 00 00 |....PK..........|
509 * ........... sig........ vmade vneed flags compr
510 * 0040 42 1a b8 3c 00 00 00 00 00 00 00 00 00 00 00 00 |B..<............|
511 * mtime mdate crc32...... csize...... usize......
512 * 0050 01 00 0d 00 00 00 00 00 00 00 00 00 a4 81 00 00 |................|
513 * fnlen exlen clen. dnum. iattr eattr...... relofs> (eattr = rw-r--r--)
514 * 0060 00 00 7a 55 54 05 00 03 cc d3 f9 4b 55 78 00 00 |..zUT......KUx..|
515 * >..... fn extra_field...........................
516 * 0070 [50 4b]05 06 00 00 00 00 01 00 01 00 3c 00 00 00 |PK..........<...|
517 * 0080 34 00 00 00 00 00 |4.....|
518 */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200519 total_usize = 0;
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000520 total_size = 0;
521 total_entries = 0;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000522#if ENABLE_DESKTOP
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200523 cdf_offset = 0;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000524#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000525 while (1) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000526 uint32_t magic;
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200527 mode_t dir_mode = 0777;
528#if ENABLE_DESKTOP
529 mode_t file_mode = 0666;
530#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000531
Paul Fox0840b762005-07-20 20:26:49 +0000532 /* Check magic number */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000533 xread(zip_fd, &magic, 4);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000534 /* Central directory? It's at the end, so exit */
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200535 if (magic == ZIP_CDF_MAGIC)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000536 break;
Denis Vlasenko006e8622008-09-21 01:01:46 +0000537#if ENABLE_DESKTOP
538 /* Data descriptor? It was a streaming file, go on */
539 if (magic == ZIP_DD_MAGIC) {
540 /* skip over duplicate crc32, cmpsize and ucmpsize */
541 unzip_skip(3 * 4);
542 continue;
543 }
544#endif
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000545 if (magic != ZIP_FILEHEADER_MAGIC)
Denis Vlasenko48a99712008-07-26 17:32:41 +0000546 bb_error_msg_and_die("invalid zip magic %08X", (int)magic);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000547
548 /* Read the file header */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000549 xread(zip_fd, zip_header.raw, ZIP_HEADER_LEN);
550 FIX_ENDIANNESS_ZIP(zip_header);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000551 if ((zip_header.formatted.method != 0) && (zip_header.formatted.method != 8)) {
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000552 bb_error_msg_and_die("unsupported method %d", zip_header.formatted.method);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000553 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000554#if !ENABLE_DESKTOP
Denys Vlasenkoe98884b2010-05-24 04:46:18 +0200555 if (zip_header.formatted.zip_flags & SWAP_LE16(0x0009)) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000556 bb_error_msg_and_die("zip flags 1 and 8 are not supported");
557 }
558#else
Denys Vlasenkoe98884b2010-05-24 04:46:18 +0200559 if (zip_header.formatted.zip_flags & SWAP_LE16(0x0001)) {
Denis Vlasenko48a99712008-07-26 17:32:41 +0000560 /* 0x0001 - encrypted */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000561 bb_error_msg_and_die("zip flag 1 (encryption) is not supported");
562 }
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200563
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200564 if (cdf_offset != BAD_CDF_OFFSET) {
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200565 cdf_header_t cdf_header;
566 cdf_offset = read_next_cdf(cdf_offset, &cdf_header);
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200567 /*
568 * Note: cdf_offset can become BAD_CDF_OFFSET after the above call.
569 */
Denys Vlasenkoe98884b2010-05-24 04:46:18 +0200570 if (zip_header.formatted.zip_flags & SWAP_LE16(0x0008)) {
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200571 /* 0x0008 - streaming. [u]cmpsize can be reliably gotten
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200572 * only from Central Directory. See unzip_doc.txt
573 */
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200574 zip_header.formatted.crc32 = cdf_header.formatted.crc32;
575 zip_header.formatted.cmpsize = cdf_header.formatted.cmpsize;
576 zip_header.formatted.ucmpsize = cdf_header.formatted.ucmpsize;
577 }
578 if ((cdf_header.formatted.version_made_by >> 8) == 3) {
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200579 /* This archive is created on Unix */
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200580 dir_mode = file_mode = (cdf_header.formatted.external_file_attributes >> 16);
581 }
Denis Vlasenko48a99712008-07-26 17:32:41 +0000582 }
Denys Vlasenko26cd90c2013-07-21 02:31:08 +0200583 if (cdf_offset == BAD_CDF_OFFSET
584 && (zip_header.formatted.zip_flags & SWAP_LE16(0x0008))
585 ) {
586 /* If it's a streaming zip, we _require_ CDF */
587 bb_error_msg_and_die("can't find file table");
588 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000589#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000590
591 /* Read filename */
Paul Fox0840b762005-07-20 20:26:49 +0000592 free(dst_fn);
"Robert P. J. Day"eea56182006-07-20 19:02:24 +0000593 dst_fn = xzalloc(zip_header.formatted.filename_len + 1);
Denis Vlasenko006e8622008-09-21 01:01:46 +0000594 xread(zip_fd, dst_fn, zip_header.formatted.filename_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000595
Paul Fox0840b762005-07-20 20:26:49 +0000596 /* Skip extra header bytes */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000597 unzip_skip(zip_header.formatted.extra_len);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000598
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100599 /* Guard against "/abspath", "/../" and similar attacks */
600 overlapping_strcpy(dst_fn, strip_unsafe_prefix(dst_fn));
601
Paul Fox0840b762005-07-20 20:26:49 +0000602 /* Filter zip entries */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000603 if (find_list_entry(zreject, dst_fn)
604 || (zaccept && !find_list_entry(zaccept, dst_fn))
605 ) { /* Skip entry */
Paul Fox0840b762005-07-20 20:26:49 +0000606 i = 'n';
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000607
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100608 } else {
609 if (listing) {
610 /* List entry */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200611 unsigned dostime = zip_header.formatted.modtime | (zip_header.formatted.moddate << 16);
612 if (!verbose) {
613 // " Length Date Time Name\n"
614 // " -------- ---- ---- ----"
615 printf( "%9u %02u-%02u-%02u %02u:%02u %s\n",
616 (unsigned)zip_header.formatted.ucmpsize,
617 (dostime & 0x01e00000) >> 21,
618 (dostime & 0x001f0000) >> 16,
619 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
620 (dostime & 0x0000f800) >> 11,
621 (dostime & 0x000007e0) >> 5,
622 dst_fn);
623 total_usize += zip_header.formatted.ucmpsize;
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000624 } else {
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200625 unsigned long percents = zip_header.formatted.ucmpsize - zip_header.formatted.cmpsize;
626 percents = percents * 100;
627 if (zip_header.formatted.ucmpsize)
628 percents /= zip_header.formatted.ucmpsize;
629 // " Length Method Size Ratio Date Time CRC-32 Name\n"
630 // "-------- ------ ------- ----- ---- ---- ------ ----"
631 printf( "%8u Defl:N" "%9u%4u%% %02u-%02u-%02u %02u:%02u %08x %s\n",
632 (unsigned)zip_header.formatted.ucmpsize,
633 (unsigned)zip_header.formatted.cmpsize,
634 (unsigned)percents,
635 (dostime & 0x01e00000) >> 21,
636 (dostime & 0x001f0000) >> 16,
637 (((dostime & 0xfe000000) >> 25) + 1980) % 100,
638 (dostime & 0x0000f800) >> 11,
639 (dostime & 0x000007e0) >> 5,
640 zip_header.formatted.crc32,
641 dst_fn);
642 total_usize += zip_header.formatted.ucmpsize;
643 total_size += zip_header.formatted.cmpsize;
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000644 }
645 i = 'n';
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100646 } else if (dst_fd == STDOUT_FILENO) {
647 /* Extracting to STDOUT */
Paul Fox0840b762005-07-20 20:26:49 +0000648 i = -1;
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100649 } else if (last_char_is(dst_fn, '/')) {
650 /* Extract directory */
Paul Fox0840b762005-07-20 20:26:49 +0000651 if (stat(dst_fn, &stat_buf) == -1) {
652 if (errno != ENOENT) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000653 bb_perror_msg_and_die("can't stat '%s'", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000654 }
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200655 if (!quiet) {
Paul Fox0840b762005-07-20 20:26:49 +0000656 printf(" creating: %s\n", dst_fn);
657 }
658 unzip_create_leading_dirs(dst_fn);
Natanael Copa02112d82012-05-22 17:11:46 +0200659 if (bb_make_directory(dst_fn, dir_mode, FILEUTILS_IGNORE_CHMOD_ERR)) {
Denys Vlasenko831756b2011-09-09 17:30:55 +0200660 xfunc_die();
Paul Fox0840b762005-07-20 20:26:49 +0000661 }
662 } else {
663 if (!S_ISDIR(stat_buf.st_mode)) {
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100664 bb_error_msg_and_die("'%s' exists but is not a %s",
665 dst_fn, "directory");
Paul Fox0840b762005-07-20 20:26:49 +0000666 }
667 }
668 i = 'n';
669
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100670 } else {
671 /* Extract file */
Denis Vlasenko48a99712008-07-26 17:32:41 +0000672 check_file:
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100673 if (stat(dst_fn, &stat_buf) == -1) {
674 /* File does not exist */
Paul Fox0840b762005-07-20 20:26:49 +0000675 if (errno != ENOENT) {
Denis Vlasenko006e8622008-09-21 01:01:46 +0000676 bb_perror_msg_and_die("can't stat '%s'", dst_fn);
Paul Fox0840b762005-07-20 20:26:49 +0000677 }
678 i = 'y';
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100679 } else {
680 /* File already exists */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000681 if (overwrite == O_NEVER) {
Paul Fox0840b762005-07-20 20:26:49 +0000682 i = 'n';
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100683 } else if (S_ISREG(stat_buf.st_mode)) {
684 /* File is regular file */
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000685 if (overwrite == O_ALWAYS) {
Paul Fox0840b762005-07-20 20:26:49 +0000686 i = 'y';
687 } else {
688 printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
Denys Vlasenkobf998072013-01-22 11:16:08 +0100689 my_fgets80(key_buf);
Paul Fox0840b762005-07-20 20:26:49 +0000690 i = key_buf[0];
691 }
Denys Vlasenko8c06bc62015-02-10 01:30:43 +0100692 } else {
693 /* File is not regular file */
694 bb_error_msg_and_die("'%s' exists but is not a %s",
695 dst_fn, "regular file");
Paul Fox0840b762005-07-20 20:26:49 +0000696 }
697 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000698 }
699 }
700
Paul Fox0840b762005-07-20 20:26:49 +0000701 switch (i) {
702 case 'A':
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000703 overwrite = O_ALWAYS;
Paul Fox0840b762005-07-20 20:26:49 +0000704 case 'y': /* Open file and fall into unzip */
705 unzip_create_leading_dirs(dst_fn);
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200706#if ENABLE_DESKTOP
707 dst_fd = xopen3(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, file_mode);
708#else
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000709 dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC);
Denys Vlasenko4e8ff732010-05-24 04:33:02 +0200710#endif
Paul Fox0840b762005-07-20 20:26:49 +0000711 case -1: /* Unzip */
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200712 if (!quiet) {
Paul Fox0840b762005-07-20 20:26:49 +0000713 printf(" inflating: %s\n", dst_fn);
714 }
Denis Vlasenko006e8622008-09-21 01:01:46 +0000715 unzip_extract(&zip_header, dst_fd);
Paul Fox0840b762005-07-20 20:26:49 +0000716 if (dst_fd != STDOUT_FILENO) {
717 /* closing STDOUT is potentially bad for future business */
718 close(dst_fd);
719 }
720 break;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000721
Paul Fox0840b762005-07-20 20:26:49 +0000722 case 'N':
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000723 overwrite = O_NEVER;
Paul Fox0840b762005-07-20 20:26:49 +0000724 case 'n':
725 /* Skip entry data */
Denis Vlasenko006e8622008-09-21 01:01:46 +0000726 unzip_skip(zip_header.formatted.cmpsize);
Paul Fox0840b762005-07-20 20:26:49 +0000727 break;
728
729 case 'r':
730 /* Prompt for new name */
731 printf("new name: ");
Denys Vlasenkobf998072013-01-22 11:16:08 +0100732 my_fgets80(key_buf);
Paul Fox0840b762005-07-20 20:26:49 +0000733 free(dst_fn);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000734 dst_fn = xstrdup(key_buf);
Paul Fox0840b762005-07-20 20:26:49 +0000735 chomp(dst_fn);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000736 goto check_file;
Paul Fox0840b762005-07-20 20:26:49 +0000737
738 default:
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200739 printf("error: invalid response [%c]\n", (char)i);
Denis Vlasenko48a99712008-07-26 17:32:41 +0000740 goto check_file;
Paul Fox0840b762005-07-20 20:26:49 +0000741 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000742
Denis Vlasenko006e8622008-09-21 01:01:46 +0000743 total_entries++;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000744 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000745
Denys Vlasenko386bc9f2009-09-06 16:52:50 +0200746 if (listing && quiet <= 1) {
747 if (!verbose) {
748 // " Length Date Time Name\n"
749 // " -------- ---- ---- ----"
750 printf( " -------- -------\n"
751 "%9lu" " %u files\n",
752 total_usize, total_entries);
753 } else {
754 unsigned long percents = total_usize - total_size;
755 percents = percents * 100;
756 if (total_usize)
757 percents /= total_usize;
758 // " Length Method Size Ratio Date Time CRC-32 Name\n"
759 // "-------- ------ ------- ----- ---- ---- ------ ----"
760 printf( "-------- ------- --- -------\n"
761 "%8lu" "%17lu%4u%% %u files\n",
762 total_usize, total_size, (unsigned)percents,
763 total_entries);
764 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000765 }
766
Denis Vlasenkobc7c5d02007-10-18 23:27:46 +0000767 return 0;
Glenn L McGrath87ac7022002-01-02 13:52:26 +0000768}