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.
11 /* Why our g[un]zip/bunzip2 are so ugly compared to this beauty? */
14 #include "unarchive.h"
16 #define UNLZMA_OPT_STDOUT 1
18 int unlzma_main(int argc, char **argv)
20 USE_DESKTOP(long long) int status;
25 opt = getopt32(argc, argv, "c");
27 /* Set input filename and number */
28 filename = argv[optind];
29 if (filename && NOT_LONE_DASH(filename)) {
31 src_fd = xopen(filename, O_RDONLY);
33 src_fd = STDIN_FILENO;
37 /* if called as lzmacat force the stdout flag */
38 if ((opt & UNLZMA_OPT_STDOUT) || applet_name[4] == 'c')
43 /* bug: char *extension = filename + strlen(filename) - 5; */
44 char *extension = strrchr(filename, '.');
45 if (!extension || strcmp(extension, ".lzma") != 0) {
46 bb_error_msg_and_die("invalid extension");
48 xstat(filename, &stat_buf);
50 dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
53 dst_fd = STDOUT_FILENO;
54 status = unlzma(src_fd, dst_fd);
56 if (status >= 0) /* if success delete src, else delete dst */
57 filename[strlen(filename)] = '.';
58 if (unlink(filename) < 0) {
59 bb_error_msg_and_die("cannot remove %s", filename);