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 = {.tv_sec = info.mtime,
110 /* Note: we closed it first.
111 * On some systems calling utime
112 * then closing resets the mtime
113 * back to current time. */
114 utimes(new_name, ×); /* ignoring errors */
117 /* Delete _compressed_ file */
119 /* restore extension (unless tgz -> tar case) */
120 if (new_name == filename)
121 filename[strlen(filename)] = '.';
125 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
126 /* Extreme bloat for gunzip compat */
127 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
128 fprintf(stderr, "%s: %u%% - replaced with %s\n",
129 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
134 if (new_name != filename)
137 } while (*argv && *++argv);
142 #if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
145 char* make_new_name_generic(char *filename, const char *expected_ext)
147 char *extension = strrchr(filename, '.');
148 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
149 /* Mimic GNU gunzip - "real" bunzip2 tries to */
150 /* unpack file anyway, to file.out */
161 * Modified for busybox by Glenn McGrath
162 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
164 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
170 char* make_new_name_bunzip2(char *filename)
172 return make_new_name_generic(filename, "bz2");
176 IF_DESKTOP(long long) int unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
178 return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
181 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
182 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
184 getopt32(argv, "cfvdt");
186 if (applet_name[2] == 'c')
187 option_mask32 |= OPT_STDOUT;
189 return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
196 * Gzip implementation for busybox
198 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
200 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
201 * based on gzip sources
203 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
204 * well as stdin/stdout, and to generally behave itself wrt command line
207 * General cleanup to better adhere to the style guide and make use of standard
208 * busybox functions by Glenn McGrath
210 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
212 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
213 * Copyright (C) 1992-1993 Jean-loup Gailly
214 * The unzip code was written and put in the public domain by Mark Adler.
215 * Portions of the lzw code are derived from the public domain 'compress'
216 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
217 * Ken Turkowski, Dave Mack and Peter Jannesen.
219 * See the license_msg below and the file COPYING for the software license.
220 * See the file algorithm.doc for the compression algorithms and file formats.
226 char* make_new_name_gunzip(char *filename)
228 char *extension = strrchr(filename, '.');
234 if (strcmp(extension, "tgz" + 1) == 0
235 #if ENABLE_FEATURE_SEAMLESS_Z
236 || (extension[0] == 'Z' && extension[1] == '\0')
239 extension[-1] = '\0';
240 } else if (strcmp(extension, "tgz") == 0) {
241 filename = xstrdup(filename);
242 extension = strrchr(filename, '.');
252 IF_DESKTOP(long long) int unpack_gunzip(unpack_info_t *info)
254 IF_DESKTOP(long long) int status = -1;
256 /* do the decompression, and cleanup */
257 if (xread_char(STDIN_FILENO) == 0x1f) {
258 unsigned char magic2;
260 magic2 = xread_char(STDIN_FILENO);
261 if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
262 status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
263 } else if (magic2 == 0x8b) {
264 status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info);
269 bb_error_msg("error inflating");
273 bb_error_msg("invalid magic");
274 /* status is still == -1 */
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.
293 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
294 int gunzip_main(int argc UNUSED_PARAM, char **argv)
296 getopt32(argv, "cfvdtn");
298 /* if called as zcat */
299 if (applet_name[1] == 'c')
300 option_mask32 |= OPT_STDOUT;
302 return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
309 * Small lzma deflate implementation.
310 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
312 * Based on bunzip.c from busybox
314 * Licensed under GPL v2, see file LICENSE in this tarball for details.
320 char* make_new_name_unlzma(char *filename)
322 return make_new_name_generic(filename, "lzma");
326 IF_DESKTOP(long long) int unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
328 return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
331 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
332 int unlzma_main(int argc UNUSED_PARAM, char **argv)
334 getopt32(argv, "cf");
337 if (applet_name[4] == 'c')
338 option_mask32 |= OPT_STDOUT;
340 return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
347 * Uncompress applet for busybox (c) 2002 Glenn McGrath
349 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
352 #if ENABLE_UNCOMPRESS
355 char* make_new_name_uncompress(char *filename)
357 return make_new_name_generic(filename, "Z");
361 IF_DESKTOP(long long) int unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
363 IF_DESKTOP(long long) int status = -1;
365 if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
366 bb_error_msg("invalid magic");
368 status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
373 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
374 int uncompress_main(int argc UNUSED_PARAM, char **argv)
376 getopt32(argv, "cf");
379 return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);