1 /* vi: set sw=4 ts=4: */
3 * Small lzma deflate implementation.
4 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
6 * Based on bunzip.c from busybox
8 * Licensed under GPL v2, see file LICENSE in this tarball for details.
18 #include "unarchive.h"
20 #define UNLZMA_OPT_STDOUT 1
22 int unlzma_main(int argc, char **argv)
26 int status, src_fd, dst_fd;
28 opt = bb_getopt_ulflags(argc, argv, "c");
30 /* Set input filename and number */
31 filename = argv[optind];
32 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
34 src_fd = bb_xopen(filename, O_RDONLY);
36 src_fd = STDIN_FILENO;
40 /* if called as lzmacat force the stdout flag */
41 if ((opt & UNLZMA_OPT_STDOUT) || bb_applet_name[4] == 'c')
46 char *extension = filename + strlen(filename) - 5;
48 if (strcmp(extension, ".lzma") != 0) {
49 bb_error_msg_and_die("Invalid extension");
51 xstat(filename, &stat_buf);
53 dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode);
55 dst_fd = STDOUT_FILENO;
56 status = unlzma(src_fd, dst_fd);
59 filename[strlen(filename)] = '.';
60 if (unlink(filename) < 0) {
61 bb_error_msg_and_die("Couldn't remove %s", filename);