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.
21 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
23 int fd = open(filename, flags, mode);
25 bb_perror_msg("%s", filename);
29 if (dup2(fd, to_fd) < 0)
30 bb_perror_msg_and_die("cannot dup");
36 int bbunpack(char **argv,
37 char* (*make_new_name)(char *filename),
38 USE_DESKTOP(long long) int (*unpacker)(void)
42 USE_DESKTOP(long long) int status;
43 char *filename, *new_name;
44 smallint exitcode = 0;
47 /* NB: new_name is *maybe* malloc'ed! */
49 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
51 if (filename && LONE_DASH(filename))
56 if (stat(filename, &stat_buf) != 0) {
57 bb_perror_msg("%s", filename);
62 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
66 /* Special cases: test, stdout */
67 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
68 if (option_mask32 & OPT_TEST)
69 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
74 /* Open dst if we are going to unpack to file */
76 new_name = make_new_name(filename);
78 bb_error_msg("%s: unknown suffix - ignored", filename);
81 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
82 /* GNU gunzip goes not bail out, but goes to next file */
83 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
88 /* Check that the input is sane */
89 if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
90 bb_error_msg_and_die("compressed data not read from terminal, "
91 "use -f to force it");
101 /* TODO: restore user/group/times here? */
102 /* Delete _compressed_ file */
104 /* restore extension (unless tgz -> tar case) */
105 if (new_name == filename)
106 filename[strlen(filename)] = '.';
109 bb_perror_msg_and_die("cannot remove %s", del);
111 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
112 /* Extreme bloat for gunzip compat */
113 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
114 fprintf(stderr, "%s: %u%% - replaced with %s\n",
115 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
120 if (new_name != filename)
123 } while (*argv && *++argv);
128 #if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
131 char* make_new_name_generic(char *filename, const char *expected_ext)
133 char *extension = strrchr(filename, '.');
134 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
135 /* Mimic GNU gunzip - "real" bunzip2 tries to */
136 /* unpack file anyway, to file.out */
147 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
148 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
150 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
156 char* make_new_name_bunzip2(char *filename)
158 return make_new_name_generic(filename, "bz2");
162 USE_DESKTOP(long long) int unpack_bunzip2(void)
164 return uncompressStream(STDIN_FILENO, STDOUT_FILENO);
167 int bunzip2_main(int argc, char **argv);
168 int bunzip2_main(int argc, char **argv)
170 getopt32(argc, argv, "cf");
172 if (applet_name[2] == 'c')
173 option_mask32 |= OPT_STDOUT;
175 return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
182 * Gzip implementation for busybox
184 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
186 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
187 * based on gzip sources
189 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
190 * well as stdin/stdout, and to generally behave itself wrt command line
193 * General cleanup to better adhere to the style guide and make use of standard
194 * busybox functions by Glenn McGrath <bug1@iinet.net.au>
196 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
198 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
199 * Copyright (C) 1992-1993 Jean-loup Gailly
200 * The unzip code was written and put in the public domain by Mark Adler.
201 * Portions of the lzw code are derived from the public domain 'compress'
202 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
203 * Ken Turkowski, Dave Mack and Peter Jannesen.
205 * See the license_msg below and the file COPYING for the software license.
206 * See the file algorithm.doc for the compression algorithms and file formats.
212 char* make_new_name_gunzip(char *filename)
214 char *extension = strrchr(filename, '.');
220 if (strcmp(extension, "tgz" + 1) == 0
221 #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
222 || strcmp(extension, "Z") == 0
225 extension[-1] = '\0';
226 } else if(strcmp(extension, "tgz") == 0) {
227 filename = xstrdup(filename);
228 extension = strrchr(filename, '.');
238 USE_DESKTOP(long long) int unpack_gunzip(void)
240 USE_DESKTOP(long long) int status = -1;
242 /* do the decompression, and cleanup */
243 if (xread_char(STDIN_FILENO) == 0x1f) {
244 unsigned char magic2;
246 magic2 = xread_char(STDIN_FILENO);
247 if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) {
248 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
249 } else if (magic2 == 0x8b) {
250 check_header_gzip_or_die(STDIN_FILENO);
251 status = inflate_gunzip(STDIN_FILENO, STDOUT_FILENO);
256 bb_error_msg("error inflating");
260 bb_error_msg("invalid magic");
261 /* status is still == -1 */
266 int gunzip_main(int argc, char **argv);
267 int gunzip_main(int argc, char **argv)
269 getopt32(argc, argv, "cfvdt");
271 /* if called as zcat */
272 if (applet_name[1] == 'c')
273 option_mask32 |= OPT_STDOUT;
275 return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
282 * Small lzma deflate implementation.
283 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
285 * Based on bunzip.c from busybox
287 * Licensed under GPL v2, see file LICENSE in this tarball for details.
293 char* make_new_name_unlzma(char *filename)
295 return make_new_name_generic(filename, "lzma");
299 USE_DESKTOP(long long) int unpack_unlzma(void)
301 return unlzma(STDIN_FILENO, STDOUT_FILENO);
304 int unlzma_main(int argc, char **argv);
305 int unlzma_main(int argc, char **argv)
307 getopt32(argc, argv, "c");
310 if (applet_name[4] == 'c')
311 option_mask32 |= OPT_STDOUT;
313 return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
320 * Uncompress applet for busybox (c) 2002 Glenn McGrath
322 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
325 #if ENABLE_UNCOMPRESS
328 char* make_new_name_uncompress(char *filename)
330 return make_new_name_generic(filename, "Z");
334 USE_DESKTOP(long long) int unpack_uncompress(void)
336 USE_DESKTOP(long long) int status = -1;
338 if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
339 bb_error_msg("invalid magic");
341 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
346 int uncompress_main(int argc, char **argv);
347 int uncompress_main(int argc, char **argv)
349 getopt32(argc, argv, "cf");
352 return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);