hush: make "exit" in trap use pre-trap exitcode - fix for nested trap
[oweals/busybox.git] / archival / bbunzip.c
index eb6f114addba16aeb22d0ccc2f12cfddc84c1cb7..87a80220969f6a1a7759d4b4910e39de472639e2 100644 (file)
@@ -4,23 +4,22 @@
  *
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
-#include "libbb.h"
-#include "bb_archive.h"
+//kbuild:lib-$(CONFIG_ZCAT) += bbunzip.o
+//kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
+//kbuild:lib-$(CONFIG_BZCAT) += bbunzip.o
+//kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
 
 /* lzop_main() uses bbunpack(), need this: */
 //kbuild:lib-$(CONFIG_LZOP) += bbunzip.o
+//kbuild:lib-$(CONFIG_LZOPCAT) += bbunzip.o
+//kbuild:lib-$(CONFIG_UNLZOP) += bbunzip.o
+/* bzip2_main() too: */
+//kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
+/* gzip_main() too: */
+//kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
 
-/* Note: must be kept in sync with archival/lzop.c */
-enum {
-       OPT_STDOUT     = 1 << 0,
-       OPT_FORCE      = 1 << 1,
-       /* only some decompressors: */
-       OPT_VERBOSE    = 1 << 2,
-       OPT_QUIET      = 1 << 3,
-       OPT_DECOMPRESS = 1 << 4,
-       OPT_TEST       = 1 << 5,
-       SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
-};
+#include "libbb.h"
+#include "bb_archive.h"
 
 static
 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
@@ -39,7 +38,7 @@ char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
 }
 
 int FAST_FUNC bbunpack(char **argv,
-       IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
+       IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_state_t *xstate),
        char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
        const char *expected_ext
 )
@@ -48,7 +47,7 @@ int FAST_FUNC bbunpack(char **argv,
        IF_DESKTOP(long long) int status = 0;
        char *filename, *new_name;
        smallint exitcode = 0;
-       transformer_aux_data_t aux;
+       transformer_state_t xstate;
 
        do {
                /* NB: new_name is *maybe* malloc'ed! */
@@ -60,7 +59,7 @@ int FAST_FUNC bbunpack(char **argv,
 
                /* Open src */
                if (filename) {
-                       if (!(option_mask32 & SEAMLESS_MAGIC)) {
+                       if (!(option_mask32 & BBUNPK_SEAMLESS_MAGIC)) {
                                if (stat(filename, &stat_buf) != 0) {
  err_name:
                                        bb_simple_perror_msg(filename);
@@ -72,21 +71,22 @@ int FAST_FUNC bbunpack(char **argv,
                                        goto err;
                        } else {
                                /* "clever zcat" with FILE */
-                               int fd = open_zipped(filename);
+                               /* fail_if_not_compressed because zcat refuses uncompressed input */
+                               int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
                                if (fd < 0)
                                        goto err_name;
                                xmove_fd(fd, STDIN_FILENO);
                        }
                } else
-               if (option_mask32 & SEAMLESS_MAGIC) {
+               if (option_mask32 & BBUNPK_SEAMLESS_MAGIC) {
                        /* "clever zcat" on stdin */
-                       if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
+                       if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
                                goto err;
                }
 
                /* Special cases: test, stdout */
-               if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
-                       if (option_mask32 & OPT_TEST)
+               if (option_mask32 & (BBUNPK_OPT_STDOUT|BBUNPK_OPT_TEST)) {
+                       if (option_mask32 & BBUNPK_OPT_TEST)
                                if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
                                        xfunc_die();
                        filename = NULL;
@@ -101,7 +101,7 @@ int FAST_FUNC bbunpack(char **argv,
                        }
 
                        /* -f: overwrite existing output files */
-                       if (option_mask32 & OPT_FORCE) {
+                       if (option_mask32 & BBUNPK_OPT_FORCE) {
                                unlink(new_name);
                        }
 
@@ -113,15 +113,17 @@ int FAST_FUNC bbunpack(char **argv,
                }
 
                /* Check that the input is sane */
-               if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
-                       bb_error_msg_and_die("compressed data not read from terminal, "
+               if (!(option_mask32 & BBUNPK_OPT_FORCE) && isatty(STDIN_FILENO)) {
+                       bb_simple_error_msg_and_die("compressed data not read from terminal, "
                                        "use -f to force it");
                }
 
-               if (!(option_mask32 & SEAMLESS_MAGIC)) {
-                       init_transformer_aux_data(&aux);
-                       aux.check_signature = 1;
-                       status = unpacker(&aux);
+               if (!(option_mask32 & BBUNPK_SEAMLESS_MAGIC)) {
+                       init_transformer_state(&xstate);
+                       /*xstate.signature_skipped = 0; - already is */
+                       /*xstate.src_fd = STDIN_FILENO; - already is */
+                       xstate.dst_fd = STDOUT_FILENO;
+                       status = unpacker(&xstate);
                        if (status < 0)
                                exitcode = 1;
                } else {
@@ -130,7 +132,7 @@ int FAST_FUNC bbunpack(char **argv,
                                xfunc_die();
                }
 
-               if (!(option_mask32 & OPT_STDOUT))
+               if (!(option_mask32 & BBUNPK_OPT_STDOUT))
                        xclose(STDOUT_FILENO); /* with error check! */
 
                if (filename) {
@@ -140,10 +142,10 @@ int FAST_FUNC bbunpack(char **argv,
                                unsigned new_name_len;
 
                                /* TODO: restore other things? */
-                               if (aux.mtime != 0) {
+                               if (xstate.mtime != 0) {
                                        struct timeval times[2];
 
-                                       times[1].tv_sec = times[0].tv_sec = aux.mtime;
+                                       times[1].tv_sec = times[0].tv_sec = xstate.mtime;
                                        times[1].tv_usec = times[0].tv_usec = 0;
                                        /* Note: we closed it first.
                                         * On some systems calling utimes
@@ -161,7 +163,7 @@ int FAST_FUNC bbunpack(char **argv,
                                }
                                /* Extreme bloat for gunzip compat */
                                /* Some users do want this info... */
-                               if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
+                               if (ENABLE_DESKTOP && (option_mask32 & BBUNPK_OPT_VERBOSE)) {
                                        unsigned percent = status
                                                ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
                                                : 0;
@@ -173,21 +175,27 @@ int FAST_FUNC bbunpack(char **argv,
                                }
                                /* Delete _source_ file */
                                del = filename;
+                               if (option_mask32 & BBUNPK_OPT_KEEP) /* ... unless -k */
+                                       del = NULL;
                        }
-                       xunlink(del);
+                       if (del)
+                               xunlink(del);
  free_name:
                        if (new_name != filename)
                                free(new_name);
                }
        } while (*argv && *++argv);
 
-       if (option_mask32 & OPT_STDOUT)
+       if (option_mask32 & BBUNPK_OPT_STDOUT)
                xclose(STDOUT_FILENO); /* with error check! */
 
        return exitcode;
 }
 
-#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
+#if ENABLE_UNCOMPRESS \
+ || ENABLE_FEATURE_BZIP2_DECOMPRESS \
+ || ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA \
+ || ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
 static
 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
 {
@@ -215,21 +223,32 @@ char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
 //usage:     "\n       -c      Write to stdout"
 //usage:     "\n       -f      Overwrite"
 
+//config:config UNCOMPRESS
+//config:      bool "uncompress (7.1 kb)"
+//config:      default n  # ancient
+//config:      help
+//config:      uncompress is used to decompress archives created by compress.
+//config:      Not much used anymore, replaced by gzip/gunzip.
+
 //applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
 //kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
 #if ENABLE_UNCOMPRESS
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
-{
-       return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
-}
 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int uncompress_main(int argc UNUSED_PARAM, char **argv)
 {
+// (N)compress 4.2.4.4:
+// -d If given, decompression is done instead
+// -c Write output on stdout, don't remove original
+// -b Parameter limits the max number of bits/code
+// -f Forces output file to be generated
+// -v Write compression statistics
+// -V Output vesion and compile options
+// -r Recursive. If a filename is a directory, descend into it and compress everything
        getopt32(argv, "cf");
+
        argv += optind;
 
-       return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
+       return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
 }
 #endif
 
@@ -259,11 +278,12 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
  * Ken Turkowski, Dave Mack and Peter Jannesen.
  */
 //usage:#define gunzip_trivial_usage
-//usage:       "[-cft] [FILE]..."
+//usage:       "[-cfkt] [FILE]..."
 //usage:#define gunzip_full_usage "\n\n"
 //usage:       "Decompress FILEs (or stdin)\n"
 //usage:     "\n       -c      Write to stdout"
 //usage:     "\n       -f      Force"
+//usage:     "\n       -k      Keep input files"
 //usage:     "\n       -t      Test file integrity"
 //usage:
 //usage:#define gunzip_example_usage
@@ -278,11 +298,31 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
 //usage:#define zcat_full_usage "\n\n"
 //usage:       "Decompress to stdout"
 
+//config:config GUNZIP
+//config:      bool "gunzip (11 kb)"
+//config:      default y
+//config:      select FEATURE_GZIP_DECOMPRESS
+//config:      help
+//config:      gunzip is used to decompress archives created by gzip.
+//config:      You can use the '-t' option to test the integrity of
+//config:      an archive, without decompressing it.
+//config:
+//config:config ZCAT
+//config:      bool "zcat (24 kb)"
+//config:      default y
+//config:      select FEATURE_GZIP_DECOMPRESS
+//config:      help
+//config:      Alias to "gunzip -c".
+//config:
+//config:config FEATURE_GUNZIP_LONG_OPTIONS
+//config:      bool "Enable long options"
+//config:      default y
+//config:      depends on (GUNZIP || ZCAT) && LONG_OPTS
+
 //applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
-//applet:IF_GUNZIP(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
-//kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
-//kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
-#if ENABLE_GUNZIP
+//               APPLET_ODDNAME:name  main    location    suid_type     help
+//applet:IF_ZCAT(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
+#if ENABLE_FEATURE_GZIP_DECOMPRESS
 static
 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
 {
@@ -308,11 +348,17 @@ char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UN
        }
        return filename;
 }
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
-{
-       return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
-}
+
+#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
+static const char gunzip_longopts[] ALIGN1 =
+       "stdout\0"              No_argument       "c"
+       "to-stdout\0"           No_argument       "c"
+       "force\0"               No_argument       "f"
+       "test\0"                No_argument       "t"
+       "no-name\0"             No_argument       "n"
+       ;
+#endif
+
 /*
  * Linux kernel build uses gzip -d -n. We accept and ignore it.
  * Man page says:
@@ -329,19 +375,23 @@ IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int gunzip_main(int argc UNUSED_PARAM, char **argv)
 {
-       getopt32(argv, "cfvqdtn");
+#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
+       getopt32long(argv, BBUNPK_OPTSTR "dtn", gunzip_longopts);
+#else
+       getopt32(argv, BBUNPK_OPTSTR "dtn");
+#endif
        argv += optind;
 
        /* If called as zcat...
         * Normally, "zcat" is just "gunzip -c".
         * But if seamless magic is enabled, then we are much more clever.
         */
-       if (applet_name[1] == 'c')
-               option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
+       if (ENABLE_ZCAT && (!ENABLE_GUNZIP || applet_name[1] == 'c'))
+               option_mask32 |= BBUNPK_OPT_STDOUT | BBUNPK_SEAMLESS_MAGIC;
 
-       return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
+       return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
 }
-#endif
+#endif /* FEATURE_GZIP_DECOMPRESS */
 
 
 /*
@@ -351,35 +401,51 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 //usage:#define bunzip2_trivial_usage
-//usage:       "[-cf] [FILE]..."
+//usage:       "[-cfk] [FILE]..."
 //usage:#define bunzip2_full_usage "\n\n"
 //usage:       "Decompress FILEs (or stdin)\n"
 //usage:     "\n       -c      Write to stdout"
 //usage:     "\n       -f      Force"
+//usage:     "\n       -k      Keep input files"
 //usage:#define bzcat_trivial_usage
 //usage:       "[FILE]..."
 //usage:#define bzcat_full_usage "\n\n"
 //usage:       "Decompress to stdout"
 
+//config:config BUNZIP2
+//config:      bool "bunzip2 (8.7 kb)"
+//config:      default y
+//config:      select FEATURE_BZIP2_DECOMPRESS
+//config:      help
+//config:      bunzip2 is a compression utility using the Burrows-Wheeler block
+//config:      sorting text compression algorithm, and Huffman coding. Compression
+//config:      is generally considerably better than that achieved by more
+//config:      conventional LZ77/LZ78-based compressors, and approaches the
+//config:      performance of the PPM family of statistical compressors.
+//config:
+//config:      Unless you have a specific application which requires bunzip2, you
+//config:      should probably say N here.
+//config:
+//config:config BZCAT
+//config:      bool "bzcat (8.7 kb)"
+//config:      default y
+//config:      select FEATURE_BZIP2_DECOMPRESS
+//config:      help
+//config:      Alias to "bunzip2 -c".
+
 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
-//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
-//kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
-//kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
-#if ENABLE_BUNZIP2
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
-{
-       return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
-}
+//                APPLET_ODDNAME:name   main     location        suid_type     help
+//applet:IF_BZCAT(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
+#if ENABLE_FEATURE_BZIP2_DECOMPRESS || ENABLE_BUNZIP2 || ENABLE_BZCAT
 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
 {
-       getopt32(argv, "cfvqdt");
+       getopt32(argv, BBUNPK_OPTSTR "dt");
        argv += optind;
-       if (applet_name[2] == 'c') /* bzcat */
-               option_mask32 |= OPT_STDOUT;
+       if (ENABLE_BZCAT && (!ENABLE_BUNZIP2 || applet_name[2] == 'c')) /* bzcat */
+               option_mask32 |= BBUNPK_OPT_STDOUT;
 
-       return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
+       return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
 }
 #endif
 
@@ -393,98 +459,141 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
  * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 //usage:#define unlzma_trivial_usage
-//usage:       "[-cf] [FILE]..."
+//usage:       "[-cfk] [FILE]..."
 //usage:#define unlzma_full_usage "\n\n"
 //usage:       "Decompress FILE (or stdin)\n"
 //usage:     "\n       -c      Write to stdout"
 //usage:     "\n       -f      Force"
+//usage:     "\n       -k      Keep input files"
 //usage:
 //usage:#define lzma_trivial_usage
-//usage:       "-d [-cf] [FILE]..."
+//usage:       "-d [-cfk] [FILE]..."
 //usage:#define lzma_full_usage "\n\n"
 //usage:       "Decompress FILE (or stdin)\n"
 //usage:     "\n       -d      Decompress"
 //usage:     "\n       -c      Write to stdout"
 //usage:     "\n       -f      Force"
+//usage:     "\n       -k      Keep input files"
 //usage:
 //usage:#define lzcat_trivial_usage
 //usage:       "[FILE]..."
 //usage:#define lzcat_full_usage "\n\n"
 //usage:       "Decompress to stdout"
-//usage:
+
+//config:config UNLZMA
+//config:      bool "unlzma (7.5 kb)"
+//config:      default y
+//config:      help
+//config:      unlzma is a compression utility using the Lempel-Ziv-Markov chain
+//config:      compression algorithm, and range coding. Compression
+//config:      is generally considerably better than that achieved by the bzip2
+//config:      compressors.
+//config:
+//config:config LZCAT
+//config:      bool "lzcat (7.5 kb)"
+//config:      default y
+//config:      help
+//config:      Alias to "unlzma -c".
+//config:
+//config:config LZMA
+//config:      bool "lzma -d"
+//config:      default y
+//config:      help
+//config:      Enable this option if you want commands like "lzma -d" to work.
+//config:      IOW: you'll get lzma applet, but it will always require -d option.
+
+//applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
+//                APPLET_ODDNAME:name   main    location        suid_type     help
+//applet:IF_LZCAT(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
+//applet:IF_LZMA( APPLET_ODDNAME(lzma,  unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
+//kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
+//kbuild:lib-$(CONFIG_LZCAT) += bbunzip.o
+//kbuild:lib-$(CONFIG_LZMA) += bbunzip.o
+#if ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA
+int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int unlzma_main(int argc UNUSED_PARAM, char **argv)
+{
+       IF_LZMA(int opts =) getopt32(argv, BBUNPK_OPTSTR "dt");
+# if ENABLE_LZMA
+       /* lzma without -d or -t? */
+       if (applet_name[2] == 'm' && !(opts & (BBUNPK_OPT_DECOMPRESS|BBUNPK_OPT_TEST)))
+               bb_show_usage();
+# endif
+       /* lzcat? */
+       if (ENABLE_LZCAT && applet_name[2] == 'c')
+               option_mask32 |= BBUNPK_OPT_STDOUT;
+
+       argv += optind;
+       return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
+}
+#endif
+
+
 //usage:#define unxz_trivial_usage
-//usage:       "[-cf] [FILE]..."
+//usage:       "[-cfk] [FILE]..."
 //usage:#define unxz_full_usage "\n\n"
 //usage:       "Decompress FILE (or stdin)\n"
 //usage:     "\n       -c      Write to stdout"
 //usage:     "\n       -f      Force"
+//usage:     "\n       -k      Keep input files"
+//usage:     "\n       -t      Test file integrity"
 //usage:
 //usage:#define xz_trivial_usage
-//usage:       "-d [-cf] [FILE]..."
+//usage:       "-d [-cfk] [FILE]..."
 //usage:#define xz_full_usage "\n\n"
 //usage:       "Decompress FILE (or stdin)\n"
 //usage:     "\n       -d      Decompress"
 //usage:     "\n       -c      Write to stdout"
 //usage:     "\n       -f      Force"
+//usage:     "\n       -k      Keep input files"
+//usage:     "\n       -t      Test file integrity"
 //usage:
 //usage:#define xzcat_trivial_usage
 //usage:       "[FILE]..."
 //usage:#define xzcat_full_usage "\n\n"
 //usage:       "Decompress to stdout"
 
-//applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
-//applet:IF_UNLZMA(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
-//applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
-//kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
-#if ENABLE_UNLZMA
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
-{
-       return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
-}
-int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int unlzma_main(int argc UNUSED_PARAM, char **argv)
-{
-       IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
-# if ENABLE_LZMA
-       /* lzma without -d or -t? */
-       if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
-               bb_show_usage();
-# endif
-       /* lzcat? */
-       if (applet_name[2] == 'c')
-               option_mask32 |= OPT_STDOUT;
-
-       argv += optind;
-       return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
-}
-#endif
-
+//config:config UNXZ
+//config:      bool "unxz (13 kb)"
+//config:      default y
+//config:      help
+//config:      unxz is a unlzma successor.
+//config:
+//config:config XZCAT
+//config:      bool "xzcat (13 kb)"
+//config:      default y
+//config:      help
+//config:      Alias to "unxz -c".
+//config:
+//config:config XZ
+//config:      bool "xz -d"
+//config:      default y
+//config:      help
+//config:      Enable this option if you want commands like "xz -d" to work.
+//config:      IOW: you'll get xz applet, but it will always require -d option.
 
 //applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
-//applet:IF_UNXZ(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
-//applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
+//                APPLET_ODDNAME:name   main  location        suid_type     help
+//applet:IF_XZCAT(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
+//applet:IF_XZ(   APPLET_ODDNAME(xz,    unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
 //kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
-#if ENABLE_UNXZ
-static
-IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
-{
-       return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
-}
+//kbuild:lib-$(CONFIG_XZCAT) += bbunzip.o
+//kbuild:lib-$(CONFIG_XZ) += bbunzip.o
+#if ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int unxz_main(int argc UNUSED_PARAM, char **argv)
 {
-       IF_XZ(int opts =) getopt32(argv, "cfvqdt");
+       IF_XZ(int opts =) getopt32(argv, BBUNPK_OPTSTR "dt");
 # if ENABLE_XZ
        /* xz without -d or -t? */
-       if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
+       if (applet_name[2] == '\0' && !(opts & (BBUNPK_OPT_DECOMPRESS|BBUNPK_OPT_TEST)))
                bb_show_usage();
 # endif
        /* xzcat? */
-       if (applet_name[2] == 'c')
-               option_mask32 |= OPT_STDOUT;
+       if (ENABLE_XZCAT && applet_name[2] == 'c')
+               option_mask32 |= BBUNPK_OPT_STDOUT;
 
        argv += optind;
-       return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
+       return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");
 }
 #endif