1 /* vi: set sw=4 ts=4: */
3 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
4 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 #include "unarchive.h"
22 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
24 int fd = open(filename, flags, mode);
26 bb_perror_msg("%s", filename);
30 if (dup2(fd, to_fd) < 0)
31 bb_perror_msg_and_die("cannot dup");
38 int unpack(char **argv,
39 char* (*make_new_name)(char *filename),
40 USE_DESKTOP(long long) int (*unpacker)(void)
44 USE_DESKTOP(long long) int status;
45 char *filename, *new_name;
46 smallint exitcode = 0;
49 /* NB: new_name is *maybe* malloc'ed! */
51 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
53 if (filename && LONE_DASH(filename))
58 if (stat(filename, &stat_buf) != 0) {
59 bb_perror_msg("%s", filename);
64 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
68 /* Special cases: test, stdout */
69 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
70 if (option_mask32 & OPT_TEST)
71 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
76 /* Open dst if we are going to unpack to file */
78 new_name = make_new_name(filename);
80 bb_error_msg("%s: unknown suffix - ignored", filename);
83 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
84 /* GNU gunzip goes 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");
101 char *del = new_name;
103 /* TODO: restore user/group/times here? */
104 /* Delete _compressed_ file */
106 /* restore extension (unless tgz -> tar case) */
107 if (new_name == filename)
108 filename[strlen(filename)] = '.';
111 bb_perror_msg_and_die("cannot remove %s", del);
113 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
114 /* Extreme bloat for gunzip compat */
115 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
116 fprintf(stderr, "%s: %u%% - replaced with %s\n",
117 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
122 if (new_name != filename)
125 } while (*argv && *++argv);
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 */
146 char* make_new_name_bunzip2(char *filename)
148 return make_new_name_generic(filename, "bz2");
152 USE_DESKTOP(long long) int unpack_bunzip2(void)
154 return uncompressStream(STDIN_FILENO, STDOUT_FILENO);
157 int bunzip2_main(int argc, char **argv);
158 int bunzip2_main(int argc, char **argv)
160 getopt32(argc, argv, "cf");
162 if (applet_name[2] == 'c')
163 option_mask32 |= OPT_STDOUT;
165 return unpack(argv, make_new_name_bunzip2, unpack_bunzip2);
172 * Gzip implementation for busybox
174 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
176 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
177 * based on gzip sources
179 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
180 * well as stdin/stdout, and to generally behave itself wrt command line
183 * General cleanup to better adhere to the style guide and make use of standard
184 * busybox functions by Glenn McGrath <bug1@iinet.net.au>
186 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
188 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
189 * Copyright (C) 1992-1993 Jean-loup Gailly
190 * The unzip code was written and put in the public domain by Mark Adler.
191 * Portions of the lzw code are derived from the public domain 'compress'
192 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
193 * Ken Turkowski, Dave Mack and Peter Jannesen.
195 * See the license_msg below and the file COPYING for the software license.
196 * See the file algorithm.doc for the compression algorithms and file formats.
202 char* make_new_name_gunzip(char *filename)
204 char *extension = strrchr(filename, '.');
210 if (strcmp(extension, "tgz" + 1) == 0
211 #ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
212 || strcmp(extension, "Z") == 0
215 extension[-1] = '\0';
216 } else if(strcmp(extension, "tgz") == 0) {
217 filename = xstrdup(filename);
218 extension = strrchr(filename, '.');
228 USE_DESKTOP(long long) int unpack_gunzip(void)
230 USE_DESKTOP(long long) int status = -1;
232 /* do the decompression, and cleanup */
233 if (xread_char(STDIN_FILENO) == 0x1f) {
234 unsigned char magic2;
236 magic2 = xread_char(STDIN_FILENO);
237 if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) {
238 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
239 } else if (magic2 == 0x8b) {
240 check_header_gzip_or_die(STDIN_FILENO);
241 status = inflate_gunzip(STDIN_FILENO, STDOUT_FILENO);
246 bb_error_msg("error inflating");
250 bb_error_msg("invalid magic");
251 /* status is still == -1 */
256 int gunzip_main(int argc, char **argv);
257 int gunzip_main(int argc, char **argv)
259 getopt32(argc, argv, "cftdv");
261 /* if called as zcat */
262 if (applet_name[1] == 'c')
263 option_mask32 |= OPT_STDOUT;
265 return unpack(argv, make_new_name_gunzip, unpack_gunzip);
272 * Small lzma deflate implementation.
273 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
275 * Based on bunzip.c from busybox
277 * Licensed under GPL v2, see file LICENSE in this tarball for details.
283 char* make_new_name_unlzma(char *filename)
285 return make_new_name_generic(filename, "lzma");
289 USE_DESKTOP(long long) int unpack_unlzma(void)
291 return unlzma(STDIN_FILENO, STDOUT_FILENO);
294 int unlzma_main(int argc, char **argv);
295 int unlzma_main(int argc, char **argv)
297 getopt32(argc, argv, "c");
300 if (applet_name[4] == 'c')
301 option_mask32 |= OPT_STDOUT;
303 return unpack(argv, make_new_name_unlzma, unpack_unlzma);
309 /* vi: set sw=4 ts=4: */
311 * Uncompress applet for busybox (c) 2002 Glenn McGrath
313 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
316 #if ENABLE_UNCOMPRESS
319 char* make_new_name_uncompress(char *filename)
321 return make_new_name_generic(filename, "Z");
325 USE_DESKTOP(long long) int unpack_uncompress(void)
327 USE_DESKTOP(long long) int status = -1;
329 if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
330 bb_error_msg("invalid magic");
332 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
337 int uncompress_main(int argc, char **argv);
338 int uncompress_main(int argc, char **argv)
340 getopt32(argc, argv, "cf");
343 return unpack(argv, make_new_name_uncompress, unpack_uncompress);