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 tarball for details.
14 /* gunzip and bunzip2 only: */
21 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
23 int fd = open3_or_warn(filename, flags, mode);
31 int FAST_FUNC bbunpack(char **argv,
32 char* (*make_new_name)(char *filename),
33 IF_DESKTOP(long long) int (*unpacker)(unpack_info_t *info)
37 IF_DESKTOP(long long) int status;
38 char *filename, *new_name;
39 smallint exitcode = 0;
43 /* NB: new_name is *maybe* malloc'ed! */
45 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
47 if (filename && LONE_DASH(filename))
52 if (stat(filename, &stat_buf) != 0) {
53 bb_simple_perror_msg(filename);
58 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
62 /* Special cases: test, stdout */
63 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
64 if (option_mask32 & OPT_TEST)
65 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
70 /* Open dst if we are going to unpack to file */
72 new_name = make_new_name(filename);
74 bb_error_msg("%s: unknown suffix - ignored", filename);
78 /* -f: overwrite existing output files */
79 if (option_mask32 & OPT_FORCE) {
83 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
84 /* GNU gunzip does not bail out, but goes to next file */
85 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
90 /* Check that the input is sane */
91 if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
92 bb_error_msg_and_die("compressed data not read from terminal, "
93 "use -f to force it");
96 /* memset(&info, 0, sizeof(info)); */
97 info.mtime = 0; /* so far it has one member only */
98 status = unpacker(&info);
101 xclose(STDOUT_FILENO); /* with error check! */
104 char *del = new_name;
106 /* TODO: restore other things? */
108 struct timeval times;
110 times.tv_sec = info.mtime;
112 /* Note: we closed it first.
113 * On some systems calling utimes
114 * then closing resets the mtime
115 * back to current time. */
116 utimes(new_name, ×); /* ignoring errors */
119 /* Delete _compressed_ file */
121 /* restore extension (unless tgz -> tar case) */
122 if (new_name == filename)
123 filename[strlen(filename)] = '.';
127 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
128 /* Extreme bloat for gunzip compat */
129 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
130 fprintf(stderr, "%s: %u%% - replaced with %s\n",
131 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
136 if (new_name != filename)
139 } while (*argv && *++argv);
144 #if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
147 char* make_new_name_generic(char *filename, const char *expected_ext)
149 char *extension = strrchr(filename, '.');
150 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
151 /* Mimic GNU gunzip - "real" bunzip2 tries to */
152 /* unpack file anyway, to file.out */
163 * Modified for busybox by Glenn McGrath
164 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
166 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
172 char* make_new_name_bunzip2(char *filename)
174 return make_new_name_generic(filename, "bz2");
178 IF_DESKTOP(long long) int unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
180 return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
183 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
184 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
186 getopt32(argv, "cfvdt");
188 if (applet_name[2] == 'c')
189 option_mask32 |= OPT_STDOUT;
191 return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
198 * Gzip implementation for busybox
200 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
202 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
203 * based on gzip sources
205 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
206 * well as stdin/stdout, and to generally behave itself wrt command line
209 * General cleanup to better adhere to the style guide and make use of standard
210 * busybox functions by Glenn McGrath
212 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
214 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
215 * Copyright (C) 1992-1993 Jean-loup Gailly
216 * The unzip code was written and put in the public domain by Mark Adler.
217 * Portions of the lzw code are derived from the public domain 'compress'
218 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
219 * Ken Turkowski, Dave Mack and Peter Jannesen.
221 * See the license_msg below and the file COPYING for the software license.
222 * See the file algorithm.doc for the compression algorithms and file formats.
228 char* make_new_name_gunzip(char *filename)
230 char *extension = strrchr(filename, '.');
236 if (strcmp(extension, "tgz" + 1) == 0
237 #if ENABLE_FEATURE_SEAMLESS_Z
238 || (extension[0] == 'Z' && extension[1] == '\0')
241 extension[-1] = '\0';
242 } else if (strcmp(extension, "tgz") == 0) {
243 filename = xstrdup(filename);
244 extension = strrchr(filename, '.');
254 IF_DESKTOP(long long) int unpack_gunzip(unpack_info_t *info)
256 IF_DESKTOP(long long) int status = -1;
258 /* do the decompression, and cleanup */
259 if (xread_char(STDIN_FILENO) == 0x1f) {
260 unsigned char magic2;
262 magic2 = xread_char(STDIN_FILENO);
263 if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
264 status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
265 } else if (magic2 == 0x8b) {
266 status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info);
271 bb_error_msg("error inflating");
275 bb_error_msg("invalid magic");
276 /* status is still == -1 */
282 * Linux kernel build uses gzip -d -n. We accept and ignore it.
285 * gzip: do not save the original file name and time stamp.
286 * (The original name is always saved if the name had to be truncated.)
287 * gunzip: do not restore the original file name/time even if present
288 * (remove only the gzip suffix from the compressed file name).
289 * This option is the default when decompressing.
291 * gzip: always save the original file name and time stamp (this is the default)
292 * gunzip: restore the original file name and time stamp if present.
295 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
296 int gunzip_main(int argc UNUSED_PARAM, char **argv)
298 getopt32(argv, "cfvdtn");
300 /* if called as zcat */
301 if (applet_name[1] == 'c')
302 option_mask32 |= OPT_STDOUT;
304 return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
311 * Small lzma deflate implementation.
312 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
314 * Based on bunzip.c from busybox
316 * Licensed under GPL v2, see file LICENSE in this tarball for details.
322 char* make_new_name_unlzma(char *filename)
324 return make_new_name_generic(filename, "lzma");
328 IF_DESKTOP(long long) int unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
330 return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
333 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
334 int unlzma_main(int argc UNUSED_PARAM, char **argv)
336 getopt32(argv, "cf");
339 if (applet_name[4] == 'c')
340 option_mask32 |= OPT_STDOUT;
342 return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
349 * Uncompress applet for busybox (c) 2002 Glenn McGrath
351 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
354 #if ENABLE_UNCOMPRESS
357 char* make_new_name_uncompress(char *filename)
359 return make_new_name_generic(filename, "Z");
363 IF_DESKTOP(long long) int unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
365 IF_DESKTOP(long long) int status = -1;
367 if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
368 bb_error_msg("invalid magic");
370 status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
375 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
376 int uncompress_main(int argc UNUSED_PARAM, char **argv)
378 getopt32(argv, "cf");
381 return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);