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')
45 char *extension = filename + strlen(filename) - 5;
47 if (strcmp(extension, ".lzma") != 0) {
48 bb_error_msg_and_die("Invalid extension");
50 xstat(filename, &stat_buf);
52 dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode);
54 dst_fd = STDOUT_FILENO;
55 status = unlzma(src_fd, dst_fd);
58 filename[strlen(filename)] = '.';
59 if (unlink(filename) < 0) {
60 bb_error_msg_and_die("Couldn't remove %s", filename);