blob: c4cc5d821bae9ada4b11186b2947966412be4195 [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 Vlasenko833d4e72010-11-03 02:38:31 +01008#include "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 Vlasenko39a04f72010-05-31 14:18:57 +020036 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(unpack_info_t *info),
37 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;
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +000045 unpack_info_t info;
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
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000101 /* memset(&info, 0, sizeof(info)); */
102 info.mtime = 0; /* so far it has one member only */
103 status = unpacker(&info);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000104 if (status < 0)
105 exitcode = 1;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200106 xclose(STDOUT_FILENO); /* with error check! */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000107
108 if (filename) {
109 char *del = new_name;
110 if (status >= 0) {
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000111 /* TODO: restore other things? */
112 if (info.mtime) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100113 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100114
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100115 times[1].tv_sec = times[0].tv_sec = info.mtime;
116 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200117 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100118 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200119 * then closing resets the mtime
120 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100121 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000122 }
123
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000124 /* Delete _compressed_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000125 del = filename;
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000126 /* restore extension (unless tgz -> tar case) */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000127 if (new_name == filename)
128 filename[strlen(filename)] = '.';
129 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000130 xunlink(del);
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000131
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000132#if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
133 /* Extreme bloat for gunzip compat */
134 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
135 fprintf(stderr, "%s: %u%% - replaced with %s\n",
136 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
137 }
138#endif
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000139
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000140 free_name:
141 if (new_name != filename)
142 free(new_name);
143 }
144 } while (*argv && *++argv);
145
146 return exitcode;
147}
148
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200149#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000150static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200151char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000152{
153 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000154 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000155 /* Mimic GNU gunzip - "real" bunzip2 tries to */
156 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000157 return NULL;
158 }
159 *extension = '\0';
160 return filename;
161}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000162#endif
163
164
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000165/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200166 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000167 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200168 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000169 */
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200170#if ENABLE_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000171static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200172IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(unpack_info_t *info UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000173{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200174 IF_DESKTOP(long long) int status = -1;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000175
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200176 if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
177 bb_error_msg("invalid magic");
178 } else {
179 status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
180 }
181 return status;
182}
183int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
184int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000185{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200186 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000187 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000188
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200189 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000190}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000191#endif
192
193
194/*
195 * Gzip implementation for busybox
196 *
197 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
198 *
199 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
200 * based on gzip sources
201 *
202 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
203 * well as stdin/stdout, and to generally behave itself wrt command line
204 * handling.
205 *
206 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000207 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000208 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200209 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000210 *
211 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
212 * Copyright (C) 1992-1993 Jean-loup Gailly
213 * The unzip code was written and put in the public domain by Mark Adler.
214 * Portions of the lzw code are derived from the public domain 'compress'
215 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
216 * Ken Turkowski, Dave Mack and Peter Jannesen.
217 *
218 * See the license_msg below and the file COPYING for the software license.
219 * See the file algorithm.doc for the compression algorithms and file formats.
220 */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000221#if ENABLE_GUNZIP
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000222static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200223char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000224{
225 char *extension = strrchr(filename, '.');
226
227 if (!extension)
228 return NULL;
229
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000230 extension++;
231 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000232#if ENABLE_FEATURE_SEAMLESS_Z
233 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000234#endif
235 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000236 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000237 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000238 filename = xstrdup(filename);
239 extension = strrchr(filename, '.');
240 extension[2] = 'a';
241 extension[3] = 'r';
242 } else {
243 return NULL;
244 }
245 return filename;
246}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000247static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200248IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(unpack_info_t *info)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000249{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000250 IF_DESKTOP(long long) int status = -1;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000251
252 /* do the decompression, and cleanup */
253 if (xread_char(STDIN_FILENO) == 0x1f) {
254 unsigned char magic2;
255
256 magic2 = xread_char(STDIN_FILENO);
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000257 if (ENABLE_FEATURE_SEAMLESS_Z && magic2 == 0x9d) {
258 status = unpack_Z_stream(STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000259 } else if (magic2 == 0x8b) {
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000260 status = unpack_gz_stream_with_info(STDIN_FILENO, STDOUT_FILENO, info);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000261 } else {
262 goto bad_magic;
263 }
264 if (status < 0) {
265 bb_error_msg("error inflating");
266 }
267 } else {
268 bad_magic:
269 bb_error_msg("invalid magic");
270 /* status is still == -1 */
271 }
272 return status;
273}
Denis Vlasenko52978092008-03-30 13:11:47 +0000274/*
275 * Linux kernel build uses gzip -d -n. We accept and ignore it.
276 * Man page says:
277 * -n --no-name
278 * gzip: do not save the original file name and time stamp.
279 * (The original name is always saved if the name had to be truncated.)
280 * gunzip: do not restore the original file name/time even if present
281 * (remove only the gzip suffix from the compressed file name).
282 * This option is the default when decompressing.
283 * -N --name
284 * gzip: always save the original file name and time stamp (this is the default)
285 * gunzip: restore the original file name and time stamp if present.
286 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000287int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000288int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000289{
Denis Vlasenko52978092008-03-30 13:11:47 +0000290 getopt32(argv, "cfvdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000291 argv += optind;
292 /* if called as zcat */
293 if (applet_name[1] == 'c')
294 option_mask32 |= OPT_STDOUT;
295
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200296 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000297}
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200298#endif
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000299
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200300
301/*
302 * Modified for busybox by Glenn McGrath
303 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
304 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200305 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200306 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200307//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200308//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200309//usage:#define bunzip2_full_usage "\n\n"
310//usage: "Decompress FILEs (or stdin)\n"
311//usage: "\nOptions:"
312//usage: "\n -c Write to stdout"
313//usage: "\n -f Force"
314//usage:#define bzcat_trivial_usage
315//usage: "FILE"
316//usage:#define bzcat_full_usage "\n\n"
317//usage: "Decompress to stdout"
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100318//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
319//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200320#if ENABLE_BUNZIP2
321static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200322IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(unpack_info_t *info UNUSED_PARAM)
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200323{
324 return unpack_bz2_stream_prime(STDIN_FILENO, STDOUT_FILENO);
325}
326int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
327int bunzip2_main(int argc UNUSED_PARAM, char **argv)
328{
329 getopt32(argv, "cfvdt");
330 argv += optind;
331 if (applet_name[2] == 'c') /* bzcat */
332 option_mask32 |= OPT_STDOUT;
333
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200334 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200335}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000336#endif
337
338
339/*
340 * Small lzma deflate implementation.
341 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
342 *
343 * Based on bunzip.c from busybox
344 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200345 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000346 */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000347#if ENABLE_UNLZMA
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000348static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200349IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(unpack_info_t *info UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000350{
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000351 return unpack_lzma_stream(STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000352}
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000353int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000354int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000355{
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200356 IF_LZMA(int opts =) getopt32(argv, "cfvdt");
357# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200358 /* lzma without -d or -t? */
359 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
360 bb_show_usage();
361# endif
362 /* lzcat? */
363 if (applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000364 option_mask32 |= OPT_STDOUT;
365
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200366 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200367 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000368}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000369#endif
370
371
Denys Vlasenko602ce692010-05-30 03:35:18 +0200372#if ENABLE_UNXZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200373static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200374IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(unpack_info_t *info UNUSED_PARAM)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200375{
Denys Vlasenko45f66162010-07-01 05:12:28 +0200376 struct {
377 uint32_t v1;
378 uint16_t v2;
379 } magic;
380 xread(STDIN_FILENO, &magic, 6);
381 if (magic.v1 != XZ_MAGIC1a || magic.v2 != XZ_MAGIC2a) {
382 bb_error_msg("invalid magic");
383 return -1;
384 }
Denys Vlasenko6948f212010-05-30 04:18:13 +0200385 return unpack_xz_stream(STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenko602ce692010-05-30 03:35:18 +0200386}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200387int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
388int unxz_main(int argc UNUSED_PARAM, char **argv)
389{
Denys Vlasenko9297dbc2010-07-05 21:37:12 +0200390 IF_XZ(int opts =) getopt32(argv, "cfvdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200391# if ENABLE_XZ
392 /* xz without -d or -t? */
393 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
394 bb_show_usage();
395# endif
396 /* xzcat? */
397 if (applet_name[2] == 'c')
398 option_mask32 |= OPT_STDOUT;
399
400 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200401 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200402}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200403#endif