1 /* vi: set sw=4 ts=4: */
3 * Common code for gunzip-like applets
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 #include "bb_archive.h"
10 /* Note: must be kept in sync with archival/lzop.c */
14 /* only some decompressors: */
17 OPT_DECOMPRESS = 1 << 4,
19 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
23 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
25 int fd = open3_or_warn(filename, flags, mode);
33 char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
35 return xasprintf("%s.%s", filename, expected_ext);
38 int FAST_FUNC bbunpack(char **argv,
39 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
40 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
41 const char *expected_ext
45 IF_DESKTOP(long long) int status = 0;
46 char *filename, *new_name;
47 smallint exitcode = 0;
48 transformer_aux_data_t aux;
51 /* NB: new_name is *maybe* malloc'ed! */
53 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
55 if (filename && LONE_DASH(filename))
60 if (!(option_mask32 & SEAMLESS_MAGIC)) {
61 if (stat(filename, &stat_buf) != 0) {
63 bb_simple_perror_msg(filename);
68 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
71 /* "clever zcat" with FILE */
72 int fd = open_zipped(filename);
75 xmove_fd(fd, STDIN_FILENO);
78 if (option_mask32 & SEAMLESS_MAGIC) {
79 /* "clever zcat" on stdin */
80 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
84 /* Special cases: test, stdout */
85 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
86 if (option_mask32 & OPT_TEST)
87 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
92 /* Open dst if we are going to unpack to file */
94 new_name = make_new_name(filename, expected_ext);
96 bb_error_msg("%s: unknown suffix - ignored", filename);
100 /* -f: overwrite existing output files */
101 if (option_mask32 & OPT_FORCE) {
105 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
106 /* GNU gunzip does not bail out, but goes to next file */
107 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
112 /* Check that the input is sane */
113 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
114 bb_error_msg_and_die("compressed data not read from terminal, "
115 "use -f to force it");
118 if (!(option_mask32 & SEAMLESS_MAGIC)) {
119 init_transformer_aux_data(&aux);
120 aux.check_signature = 1;
121 status = unpacker(&aux);
125 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
126 /* Disk full, tty closed, etc. No point in continuing */
130 if (!(option_mask32 & OPT_STDOUT))
131 xclose(STDOUT_FILENO); /* with error check! */
134 char *del = new_name;
136 /* TODO: restore other things? */
137 if (aux.mtime != 0) {
138 struct timeval times[2];
140 times[1].tv_sec = times[0].tv_sec = aux.mtime;
141 times[1].tv_usec = times[0].tv_usec = 0;
142 /* Note: we closed it first.
143 * On some systems calling utimes
144 * then closing resets the mtime
145 * back to current time. */
146 utimes(new_name, times); /* ignoring errors */
149 /* Delete _compressed_ file */
151 /* restore extension (unless tgz -> tar case) */
152 if (new_name == filename)
153 filename[strlen(filename)] = '.';
157 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
158 /* Extreme bloat for gunzip compat */
159 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
160 fprintf(stderr, "%s: %u%% - replaced with %s\n",
161 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
166 if (new_name != filename)
169 } while (*argv && *++argv);
171 if (option_mask32 & OPT_STDOUT)
172 xclose(STDOUT_FILENO); /* with error check! */
177 #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
179 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
181 char *extension = strrchr(filename, '.');
182 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
183 /* Mimic GNU gunzip - "real" bunzip2 tries to */
184 /* unpack file anyway, to file.out */
194 * Uncompress applet for busybox (c) 2002 Glenn McGrath
196 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
199 //usage:#define uncompress_trivial_usage
200 //usage: "[-cf] [FILE]..."
201 //usage:#define uncompress_full_usage "\n\n"
202 //usage: "Decompress .Z file[s]\n"
203 //usage: "\n -c Write to stdout"
204 //usage: "\n -f Overwrite"
206 #if ENABLE_UNCOMPRESS
208 IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
210 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
212 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
213 int uncompress_main(int argc UNUSED_PARAM, char **argv)
215 getopt32(argv, "cf");
218 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
224 * Gzip implementation for busybox
226 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
228 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
229 * based on gzip sources
231 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
232 * well as stdin/stdout, and to generally behave itself wrt command line
235 * General cleanup to better adhere to the style guide and make use of standard
236 * busybox functions by Glenn McGrath
238 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
240 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
241 * Copyright (C) 1992-1993 Jean-loup Gailly
242 * The unzip code was written and put in the public domain by Mark Adler.
243 * Portions of the lzw code are derived from the public domain 'compress'
244 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
245 * Ken Turkowski, Dave Mack and Peter Jannesen.
247 * See the license_msg below and the file COPYING for the software license.
248 * See the file algorithm.doc for the compression algorithms and file formats.
251 //usage:#define gunzip_trivial_usage
252 //usage: "[-cft] [FILE]..."
253 //usage:#define gunzip_full_usage "\n\n"
254 //usage: "Decompress FILEs (or stdin)\n"
255 //usage: "\n -c Write to stdout"
256 //usage: "\n -f Force"
257 //usage: "\n -t Test file integrity"
259 //usage:#define gunzip_example_usage
260 //usage: "$ ls -la /tmp/BusyBox*\n"
261 //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
262 //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
263 //usage: "$ ls -la /tmp/BusyBox*\n"
264 //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
266 //usage:#define zcat_trivial_usage
268 //usage:#define zcat_full_usage "\n\n"
269 //usage: "Decompress to stdout"
273 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
275 char *extension = strrchr(filename, '.');
281 if (strcmp(extension, "tgz" + 1) == 0
282 #if ENABLE_FEATURE_SEAMLESS_Z
283 || (extension[0] == 'Z' && extension[1] == '\0')
286 extension[-1] = '\0';
287 } else if (strcmp(extension, "tgz") == 0) {
288 filename = xstrdup(filename);
289 extension = strrchr(filename, '.');
298 IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
300 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
303 * Linux kernel build uses gzip -d -n. We accept and ignore it.
306 * gzip: do not save the original file name and time stamp.
307 * (The original name is always saved if the name had to be truncated.)
308 * gunzip: do not restore the original file name/time even if present
309 * (remove only the gzip suffix from the compressed file name).
310 * This option is the default when decompressing.
312 * gzip: always save the original file name and time stamp (this is the default)
313 * gunzip: restore the original file name and time stamp if present.
315 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
316 int gunzip_main(int argc UNUSED_PARAM, char **argv)
318 getopt32(argv, "cfvqdtn");
321 /* If called as zcat...
322 * Normally, "zcat" is just "gunzip -c".
323 * But if seamless magic is enabled, then we are much more clever.
325 if (applet_name[1] == 'c')
326 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
328 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
334 * Modified for busybox by Glenn McGrath
335 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
337 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
339 //usage:#define bunzip2_trivial_usage
340 //usage: "[-cf] [FILE]..."
341 //usage:#define bunzip2_full_usage "\n\n"
342 //usage: "Decompress FILEs (or stdin)\n"
343 //usage: "\n -c Write to stdout"
344 //usage: "\n -f Force"
345 //usage:#define bzcat_trivial_usage
347 //usage:#define bzcat_full_usage "\n\n"
348 //usage: "Decompress to stdout"
349 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
350 //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
353 IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
355 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
357 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
358 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
360 getopt32(argv, "cfvqdt");
362 if (applet_name[2] == 'c') /* bzcat */
363 option_mask32 |= OPT_STDOUT;
365 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
371 * Small lzma deflate implementation.
372 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
374 * Based on bunzip.c from busybox
376 * Licensed under GPLv2, see file LICENSE in this source tree.
379 //usage:#define unlzma_trivial_usage
380 //usage: "[-cf] [FILE]..."
381 //usage:#define unlzma_full_usage "\n\n"
382 //usage: "Decompress FILE (or stdin)\n"
383 //usage: "\n -c Write to stdout"
384 //usage: "\n -f Force"
386 //usage:#define lzma_trivial_usage
387 //usage: "-d [-cf] [FILE]..."
388 //usage:#define lzma_full_usage "\n\n"
389 //usage: "Decompress FILE (or stdin)\n"
390 //usage: "\n -d Decompress"
391 //usage: "\n -c Write to stdout"
392 //usage: "\n -f Force"
394 //usage:#define lzcat_trivial_usage
396 //usage:#define lzcat_full_usage "\n\n"
397 //usage: "Decompress to stdout"
399 //usage:#define unxz_trivial_usage
400 //usage: "[-cf] [FILE]..."
401 //usage:#define unxz_full_usage "\n\n"
402 //usage: "Decompress FILE (or stdin)\n"
403 //usage: "\n -c Write to stdout"
404 //usage: "\n -f Force"
406 //usage:#define xz_trivial_usage
407 //usage: "-d [-cf] [FILE]..."
408 //usage:#define xz_full_usage "\n\n"
409 //usage: "Decompress FILE (or stdin)\n"
410 //usage: "\n -d Decompress"
411 //usage: "\n -c Write to stdout"
412 //usage: "\n -f Force"
414 //usage:#define xzcat_trivial_usage
416 //usage:#define xzcat_full_usage "\n\n"
417 //usage: "Decompress to stdout"
421 IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
423 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
425 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
426 int unlzma_main(int argc UNUSED_PARAM, char **argv)
428 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
430 /* lzma without -d or -t? */
431 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
435 if (applet_name[2] == 'c')
436 option_mask32 |= OPT_STDOUT;
439 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
446 IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
448 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
450 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
451 int unxz_main(int argc UNUSED_PARAM, char **argv)
453 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
455 /* xz without -d or -t? */
456 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
460 if (applet_name[2] == 'c')
461 option_mask32 |= OPT_STDOUT;
464 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");