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"
13 /* only some decompressors: */
15 OPT_DECOMPRESS = 1 << 3,
20 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
22 int fd = open3_or_warn(filename, flags, mode);
30 char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
32 return xasprintf("%s.%s", filename, expected_ext);
35 int FAST_FUNC bbunpack(char **argv,
36 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
37 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
38 const char *expected_ext
42 IF_DESKTOP(long long) int status;
43 char *filename, *new_name;
44 smallint exitcode = 0;
45 transformer_aux_data_t aux;
48 /* NB: new_name is *maybe* malloc'ed! */
50 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
52 if (filename && LONE_DASH(filename))
57 if (stat(filename, &stat_buf) != 0) {
58 bb_simple_perror_msg(filename);
63 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
67 /* Special cases: test, stdout */
68 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
69 if (option_mask32 & OPT_TEST)
70 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
75 /* Open dst if we are going to unpack to file */
77 new_name = make_new_name(filename, expected_ext);
79 bb_error_msg("%s: unknown suffix - ignored", filename);
83 /* -f: overwrite existing output files */
84 if (option_mask32 & OPT_FORCE) {
88 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
89 /* GNU gunzip does not bail out, but goes to next file */
90 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
95 /* Check that the input is sane */
96 if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
97 bb_error_msg_and_die("compressed data not read from terminal, "
98 "use -f to force it");
101 init_transformer_aux_data(&aux);
102 aux.check_signature = 1;
103 status = unpacker(&aux);
107 if (!(option_mask32 & OPT_STDOUT))
108 xclose(STDOUT_FILENO); /* with error check! */
111 char *del = new_name;
113 /* TODO: restore other things? */
114 if (aux.mtime != 0) {
115 struct timeval times[2];
117 times[1].tv_sec = times[0].tv_sec = aux.mtime;
118 times[1].tv_usec = times[0].tv_usec = 0;
119 /* Note: we closed it first.
120 * On some systems calling utimes
121 * then closing resets the mtime
122 * back to current time. */
123 utimes(new_name, times); /* ignoring errors */
126 /* Delete _compressed_ file */
128 /* restore extension (unless tgz -> tar case) */
129 if (new_name == filename)
130 filename[strlen(filename)] = '.';
134 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
135 /* Extreme bloat for gunzip compat */
136 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
137 fprintf(stderr, "%s: %u%% - replaced with %s\n",
138 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
143 if (new_name != filename)
146 } while (*argv && *++argv);
148 if (option_mask32 & OPT_STDOUT)
149 xclose(STDOUT_FILENO); /* with error check! */
154 #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
156 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
158 char *extension = strrchr(filename, '.');
159 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
160 /* Mimic GNU gunzip - "real" bunzip2 tries to */
161 /* unpack file anyway, to file.out */
171 * Uncompress applet for busybox (c) 2002 Glenn McGrath
173 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
176 //usage:#define uncompress_trivial_usage
177 //usage: "[-cf] [FILE]..."
178 //usage:#define uncompress_full_usage "\n\n"
179 //usage: "Decompress .Z file[s]\n"
180 //usage: "\n -c Write to stdout"
181 //usage: "\n -f Overwrite"
183 #if ENABLE_UNCOMPRESS
185 IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
187 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
189 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
190 int uncompress_main(int argc UNUSED_PARAM, char **argv)
192 getopt32(argv, "cf");
195 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
201 * Gzip implementation for busybox
203 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
205 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
206 * based on gzip sources
208 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
209 * well as stdin/stdout, and to generally behave itself wrt command line
212 * General cleanup to better adhere to the style guide and make use of standard
213 * busybox functions by Glenn McGrath
215 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
217 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
218 * Copyright (C) 1992-1993 Jean-loup Gailly
219 * The unzip code was written and put in the public domain by Mark Adler.
220 * Portions of the lzw code are derived from the public domain 'compress'
221 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
222 * Ken Turkowski, Dave Mack and Peter Jannesen.
224 * See the license_msg below and the file COPYING for the software license.
225 * See the file algorithm.doc for the compression algorithms and file formats.
228 //usage:#define gunzip_trivial_usage
229 //usage: "[-cft] [FILE]..."
230 //usage:#define gunzip_full_usage "\n\n"
231 //usage: "Decompress FILEs (or stdin)\n"
232 //usage: "\n -c Write to stdout"
233 //usage: "\n -f Force"
234 //usage: "\n -t Test file integrity"
236 //usage:#define gunzip_example_usage
237 //usage: "$ ls -la /tmp/BusyBox*\n"
238 //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
239 //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
240 //usage: "$ ls -la /tmp/BusyBox*\n"
241 //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
243 //usage:#define zcat_trivial_usage
245 //usage:#define zcat_full_usage "\n\n"
246 //usage: "Decompress to stdout"
250 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
252 char *extension = strrchr(filename, '.');
258 if (strcmp(extension, "tgz" + 1) == 0
259 #if ENABLE_FEATURE_SEAMLESS_Z
260 || (extension[0] == 'Z' && extension[1] == '\0')
263 extension[-1] = '\0';
264 } else if (strcmp(extension, "tgz") == 0) {
265 filename = xstrdup(filename);
266 extension = strrchr(filename, '.');
275 IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
277 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
280 * Linux kernel build uses gzip -d -n. We accept and ignore it.
283 * gzip: do not save the original file name and time stamp.
284 * (The original name is always saved if the name had to be truncated.)
285 * gunzip: do not restore the original file name/time even if present
286 * (remove only the gzip suffix from the compressed file name).
287 * This option is the default when decompressing.
289 * gzip: always save the original file name and time stamp (this is the default)
290 * gunzip: restore the original file name and time stamp if present.
292 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
293 int gunzip_main(int argc UNUSED_PARAM, char **argv)
295 getopt32(argv, "cfvdtn");
297 /* if called as zcat */
298 if (applet_name[1] == 'c')
299 option_mask32 |= OPT_STDOUT;
301 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
307 * Modified for busybox by Glenn McGrath
308 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
310 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
312 //usage:#define bunzip2_trivial_usage
313 //usage: "[-cf] [FILE]..."
314 //usage:#define bunzip2_full_usage "\n\n"
315 //usage: "Decompress FILEs (or stdin)\n"
316 //usage: "\n -c Write to stdout"
317 //usage: "\n -f Force"
318 //usage:#define bzcat_trivial_usage
320 //usage:#define bzcat_full_usage "\n\n"
321 //usage: "Decompress to stdout"
322 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
323 //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
326 IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
328 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
330 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
331 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
333 getopt32(argv, "cfvdt");
335 if (applet_name[2] == 'c') /* bzcat */
336 option_mask32 |= OPT_STDOUT;
338 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
344 * Small lzma deflate implementation.
345 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
347 * Based on bunzip.c from busybox
349 * Licensed under GPLv2, see file LICENSE in this source tree.
352 //usage:#define unlzma_trivial_usage
353 //usage: "[-cf] [FILE]..."
354 //usage:#define unlzma_full_usage "\n\n"
355 //usage: "Decompress FILE (or stdin)\n"
356 //usage: "\n -c Write to stdout"
357 //usage: "\n -f Force"
359 //usage:#define lzma_trivial_usage
360 //usage: "-d [-cf] [FILE]..."
361 //usage:#define lzma_full_usage "\n\n"
362 //usage: "Decompress FILE (or stdin)\n"
363 //usage: "\n -d Decompress"
364 //usage: "\n -c Write to stdout"
365 //usage: "\n -f Force"
367 //usage:#define lzcat_trivial_usage
369 //usage:#define lzcat_full_usage "\n\n"
370 //usage: "Decompress to stdout"
372 //usage:#define unxz_trivial_usage
373 //usage: "[-cf] [FILE]..."
374 //usage:#define unxz_full_usage "\n\n"
375 //usage: "Decompress FILE (or stdin)\n"
376 //usage: "\n -c Write to stdout"
377 //usage: "\n -f Force"
379 //usage:#define xz_trivial_usage
380 //usage: "-d [-cf] [FILE]..."
381 //usage:#define xz_full_usage "\n\n"
382 //usage: "Decompress FILE (or stdin)\n"
383 //usage: "\n -d Decompress"
384 //usage: "\n -c Write to stdout"
385 //usage: "\n -f Force"
387 //usage:#define xzcat_trivial_usage
389 //usage:#define xzcat_full_usage "\n\n"
390 //usage: "Decompress to stdout"
394 IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
396 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
398 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
399 int unlzma_main(int argc UNUSED_PARAM, char **argv)
401 IF_LZMA(int opts =) getopt32(argv, "cfvdt");
403 /* lzma without -d or -t? */
404 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
408 if (applet_name[2] == 'c')
409 option_mask32 |= OPT_STDOUT;
412 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
419 IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
421 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
423 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
424 int unxz_main(int argc UNUSED_PARAM, char **argv)
426 IF_XZ(int opts =) getopt32(argv, "cfvdt");
428 /* xz without -d or -t? */
429 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
433 if (applet_name[2] == 'c')
434 option_mask32 |= OPT_STDOUT;
437 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");