blob: 94d8a81c94d4a68bf4a2ba7c3e31099729c8423a [file] [log] [blame]
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Common code for gunzip-like applets
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00006 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00007#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +02008#include "bb_archive.h"
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00009
10enum {
Denys Vlasenkoccb88a62010-05-27 02:22:54 +020011 OPT_STDOUT = 1 << 0,
12 OPT_FORCE = 1 << 1,
13 /* only some decompressors: */
14 OPT_VERBOSE = 1 << 2,
15 OPT_DECOMPRESS = 1 << 3,
16 OPT_TEST = 1 << 4,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000017};
18
19static
20int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
21{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000022 int fd = open3_or_warn(filename, flags, mode);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000023 if (fd < 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000024 return 1;
25 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +000026 xmove_fd(fd, to_fd);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000027 return 0;
28}
29
Denys Vlasenko39a04f72010-05-31 14:18:57 +020030char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
31{
32 return xasprintf("%s.%s", filename, expected_ext);
33}
34
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000035int FAST_FUNC bbunpack(char **argv,
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010036 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
Denys Vlasenko39a04f72010-05-31 14:18:57 +020037 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
38 const char *expected_ext
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000039)
40{
41 struct stat stat_buf;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000042 IF_DESKTOP(long long) int status;
Denis Vlasenko6c939e02007-03-07 23:22:47 +000043 char *filename, *new_name;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000044 smallint exitcode = 0;
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010045 transformer_aux_data_t aux;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000046
47 do {
Denis Vlasenko6c939e02007-03-07 23:22:47 +000048 /* NB: new_name is *maybe* malloc'ed! */
49 new_name = NULL;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000050 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
Denis Vlasenko6c939e02007-03-07 23:22:47 +000051
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000052 if (filename && LONE_DASH(filename))
53 filename = NULL;
54
55 /* Open src */
56 if (filename) {
57 if (stat(filename, &stat_buf) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000058 bb_simple_perror_msg(filename);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000059 err:
60 exitcode = 1;
61 goto free_name;
62 }
63 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
64 goto err;
65 }
66
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))
71 goto err;
72 filename = NULL;
73 }
74
Denis Vlasenko6c939e02007-03-07 23:22:47 +000075 /* Open dst if we are going to unpack to file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000076 if (filename) {
Denys Vlasenko39a04f72010-05-31 14:18:57 +020077 new_name = make_new_name(filename, expected_ext);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000078 if (!new_name) {
79 bb_error_msg("%s: unknown suffix - ignored", filename);
80 goto err;
81 }
Denis Vlasenkoa04cc472008-06-25 20:54:45 +000082
83 /* -f: overwrite existing output files */
84 if (option_mask32 & OPT_FORCE) {
85 unlink(new_name);
86 }
87
Denis Vlasenko6c939e02007-03-07 23:22:47 +000088 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000089 /* GNU gunzip does not bail out, but goes to next file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000090 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
91 stat_buf.st_mode))
92 goto err;
93 }
94
Denis Vlasenko6c939e02007-03-07 23:22:47 +000095 /* Check that the input is sane */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000096 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");
99 }
100
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100101 init_transformer_aux_data(&aux);
102 aux.check_signature = 1;
103 status = unpacker(&aux);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000104 if (status < 0)
105 exitcode = 1;
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100106
107 if (!(option_mask32 & OPT_STDOUT))
108 xclose(STDOUT_FILENO); /* with error check! */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000109
110 if (filename) {
111 char *del = new_name;
112 if (status >= 0) {
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000113 /* TODO: restore other things? */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100114 if (aux.mtime != 0) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100115 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100116
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100117 times[1].tv_sec = times[0].tv_sec = aux.mtime;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100118 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200119 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100120 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200121 * then closing resets the mtime
122 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100123 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000124 }
125
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000126 /* Delete _compressed_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000127 del = filename;
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000128 /* restore extension (unless tgz -> tar case) */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000129 if (new_name == filename)
130 filename[strlen(filename)] = '.';
131 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000132 xunlink(del);
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000133
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000134#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);
139 }
140#endif
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000141
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000142 free_name:
143 if (new_name != filename)
144 free(new_name);
145 }
146 } while (*argv && *++argv);
147
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100148 if (option_mask32 & OPT_STDOUT)
149 xclose(STDOUT_FILENO); /* with error check! */
150
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000151 return exitcode;
152}
153
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200154#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000155static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200156char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000157{
158 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000159 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000160 /* Mimic GNU gunzip - "real" bunzip2 tries to */
161 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000162 return NULL;
163 }
164 *extension = '\0';
165 return filename;
166}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000167#endif
168
169
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000170/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200171 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000172 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200173 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000174 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200175
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"
Pere Orga1f4447b2011-03-27 22:40:30 +0200180//usage: "\n -c Write to stdout"
181//usage: "\n -f Overwrite"
182
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200183#if ENABLE_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000184static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100185IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000186{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100187 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200188}
189int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
190int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000191{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200192 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000193 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000194
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200195 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000196}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000197#endif
198
199
200/*
201 * Gzip implementation for busybox
202 *
203 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
204 *
205 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
206 * based on gzip sources
207 *
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
210 * handling.
211 *
212 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000213 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000214 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200215 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000216 *
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.
223 *
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.
226 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200227
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"
Pere Orga1f4447b2011-03-27 22:40:30 +0200232//usage: "\n -c Write to stdout"
233//usage: "\n -f Force"
234//usage: "\n -t Test file integrity"
235//usage:
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"
242//usage:
243//usage:#define zcat_trivial_usage
244//usage: "FILE"
245//usage:#define zcat_full_usage "\n\n"
246//usage: "Decompress to stdout"
247
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000248#if ENABLE_GUNZIP
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000249static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200250char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000251{
252 char *extension = strrchr(filename, '.');
253
254 if (!extension)
255 return NULL;
256
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000257 extension++;
258 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000259#if ENABLE_FEATURE_SEAMLESS_Z
260 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000261#endif
262 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000263 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000264 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000265 filename = xstrdup(filename);
266 extension = strrchr(filename, '.');
267 extension[2] = 'a';
268 extension[3] = 'r';
269 } else {
270 return NULL;
271 }
272 return filename;
273}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000274static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100275IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000276{
Denys Vlasenko02c3c382012-03-06 16:32:06 +0100277 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000278}
Denis Vlasenko52978092008-03-30 13:11:47 +0000279/*
280 * Linux kernel build uses gzip -d -n. We accept and ignore it.
281 * Man page says:
282 * -n --no-name
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.
288 * -N --name
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.
291 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000292int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000293int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000294{
Denis Vlasenko52978092008-03-30 13:11:47 +0000295 getopt32(argv, "cfvdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000296 argv += optind;
297 /* if called as zcat */
298 if (applet_name[1] == 'c')
299 option_mask32 |= OPT_STDOUT;
300
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200301 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000302}
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200303#endif
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000304
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200305
306/*
307 * Modified for busybox by Glenn McGrath
308 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
309 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200310 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200311 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200312//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200313//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200314//usage:#define bunzip2_full_usage "\n\n"
315//usage: "Decompress FILEs (or stdin)\n"
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200316//usage: "\n -c Write to stdout"
317//usage: "\n -f Force"
318//usage:#define bzcat_trivial_usage
319//usage: "FILE"
320//usage:#define bzcat_full_usage "\n\n"
321//usage: "Decompress to stdout"
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100322//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))
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200324#if ENABLE_BUNZIP2
325static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100326IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200327{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100328 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200329}
330int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
331int bunzip2_main(int argc UNUSED_PARAM, char **argv)
332{
333 getopt32(argv, "cfvdt");
334 argv += optind;
335 if (applet_name[2] == 'c') /* bzcat */
336 option_mask32 |= OPT_STDOUT;
337
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200338 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200339}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000340#endif
341
342
343/*
344 * Small lzma deflate implementation.
345 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
346 *
347 * Based on bunzip.c from busybox
348 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200349 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000350 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200351
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"
Pere Orga1f4447b2011-03-27 22:40:30 +0200356//usage: "\n -c Write to stdout"
357//usage: "\n -f Force"
358//usage:
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"
Pere Orga1f4447b2011-03-27 22:40:30 +0200363//usage: "\n -d Decompress"
364//usage: "\n -c Write to stdout"
365//usage: "\n -f Force"
366//usage:
367//usage:#define lzcat_trivial_usage
368//usage: "FILE"
369//usage:#define lzcat_full_usage "\n\n"
370//usage: "Decompress to stdout"
371//usage:
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"
Pere Orga1f4447b2011-03-27 22:40:30 +0200376//usage: "\n -c Write to stdout"
377//usage: "\n -f Force"
378//usage:
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"
Pere Orga1f4447b2011-03-27 22:40:30 +0200383//usage: "\n -d Decompress"
384//usage: "\n -c Write to stdout"
385//usage: "\n -f Force"
386//usage:
387//usage:#define xzcat_trivial_usage
388//usage: "FILE"
389//usage:#define xzcat_full_usage "\n\n"
390//usage: "Decompress to stdout"
391
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000392#if ENABLE_UNLZMA
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000393static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100394IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000395{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100396 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000397}
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000398int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000399int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000400{
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200401 IF_LZMA(int opts =) getopt32(argv, "cfvdt");
402# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200403 /* lzma without -d or -t? */
404 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
405 bb_show_usage();
406# endif
407 /* lzcat? */
408 if (applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000409 option_mask32 |= OPT_STDOUT;
410
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200411 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200412 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000413}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000414#endif
415
416
Denys Vlasenko602ce692010-05-30 03:35:18 +0200417#if ENABLE_UNXZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200418static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100419IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200420{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100421 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenko602ce692010-05-30 03:35:18 +0200422}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200423int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
424int unxz_main(int argc UNUSED_PARAM, char **argv)
425{
Denys Vlasenko9297dbc2010-07-05 21:37:12 +0200426 IF_XZ(int opts =) getopt32(argv, "cfvdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200427# if ENABLE_XZ
428 /* xz without -d or -t? */
429 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
430 bb_show_usage();
431# endif
432 /* xzcat? */
433 if (applet_name[2] == 'c')
434 option_mask32 |= OPT_STDOUT;
435
436 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200437 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200438}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200439#endif