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.
12 #include "unarchive.h"
14 #define UNLZMA_OPT_STDOUT 1
16 int unlzma_main(int argc, char **argv)
20 int status, src_fd, dst_fd;
22 opt = bb_getopt_ulflags(argc, argv, "c");
24 /* Set input filename and number */
25 filename = argv[optind];
26 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
28 src_fd = xopen(filename, O_RDONLY);
30 src_fd = STDIN_FILENO;
34 /* if called as lzmacat force the stdout flag */
35 if ((opt & UNLZMA_OPT_STDOUT) || bb_applet_name[4] == 'c')
40 /* bug: char *extension = filename + strlen(filename) - 5; */
41 char *extension = strrchr(filename, '.');
42 if (!extension || strcmp(extension, ".lzma") != 0) {
43 bb_error_msg_and_die("invalid extension");
45 xstat(filename, &stat_buf);
47 dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
50 dst_fd = STDOUT_FILENO;
51 status = unlzma(src_fd, dst_fd);
54 filename[strlen(filename)] = '.';
55 if (unlink(filename) < 0) {
56 bb_error_msg_and_die("cannot remove %s", filename);