2 * Small lzma deflate implementation.
3 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 * Based on bunzip.c from busybox
7 * Licensed under GPL v2, see file LICENSE in this tarball for details.
17 #include "unarchive.h"
19 #define UNLZMA_OPT_STDOUT 1
21 int unlzma_main(int argc, char **argv)
25 int status, src_fd, dst_fd;
27 opt = bb_getopt_ulflags(argc, argv, "c");
29 /* Set input filename and number */
30 filename = argv[optind];
31 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
33 src_fd = bb_xopen(filename, O_RDONLY);
35 src_fd = STDIN_FILENO;
39 /* if called as lzmacat force the stdout flag */
40 if ((opt & UNLZMA_OPT_STDOUT) || bb_applet_name[4] == 'c')
44 char *extension = filename + strlen(filename) - 5;
46 if (strcmp(extension, ".lzma") != 0) {
47 bb_error_msg_and_die("Invalid extension");
50 dst_fd = bb_xopen(filename, O_WRONLY | O_CREAT);
52 dst_fd = STDOUT_FILENO;
53 status = unlzma(src_fd, dst_fd);
56 filename[strlen(filename)] = '.';
57 if (unlink(filename) < 0) {
58 bb_error_msg_and_die("Couldn't remove %s", filename);