"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Small lzma deflate implementation. |
| 4 | * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
| 5 | * |
| 6 | * Based on bunzip.c from busybox |
| 7 | * |
| 8 | * Licensed under GPL v2, see file LICENSE in this tarball for details. |
| 9 | */ |
| 10 | |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 11 | #include "busybox.h" |
| 12 | #include "unarchive.h" |
| 13 | |
| 14 | #define UNLZMA_OPT_STDOUT 1 |
| 15 | |
| 16 | int unlzma_main(int argc, char **argv) |
| 17 | { |
| 18 | char *filename; |
| 19 | unsigned long opt; |
| 20 | int status, src_fd, dst_fd; |
| 21 | |
| 22 | opt = bb_getopt_ulflags(argc, argv, "c"); |
| 23 | |
| 24 | /* Set input filename and number */ |
| 25 | filename = argv[optind]; |
| 26 | if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) { |
| 27 | /* Open input file */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 28 | src_fd = xopen(filename, O_RDONLY); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 29 | } else { |
| 30 | src_fd = STDIN_FILENO; |
| 31 | filename = 0; |
| 32 | } |
| 33 | |
| 34 | /* if called as lzmacat force the stdout flag */ |
| 35 | if ((opt & UNLZMA_OPT_STDOUT) || bb_applet_name[4] == 'c') |
| 36 | filename = 0; |
| 37 | |
| 38 | if (filename) { |
Rob Landley | 9a202c9 | 2006-06-13 14:54:42 +0000 | [diff] [blame] | 39 | struct stat stat_buf; |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 40 | /* bug: char *extension = filename + strlen(filename) - 5; */ |
| 41 | char *extension = strrchr(filename, '.'); |
| 42 | if (!extension || strcmp(extension, ".lzma") != 0) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 43 | bb_error_msg_and_die("Invalid extension"); |
| 44 | } |
Rob Landley | c4b6739 | 2006-06-13 16:09:16 +0000 | [diff] [blame] | 45 | xstat(filename, &stat_buf); |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 46 | *extension = '\0'; |
| 47 | dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC, |
| 48 | stat_buf.st_mode); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 49 | } else |
| 50 | dst_fd = STDOUT_FILENO; |
| 51 | status = unlzma(src_fd, dst_fd); |
| 52 | if (filename) { |
| 53 | if (!status) |
| 54 | filename[strlen(filename)] = '.'; |
| 55 | if (unlink(filename) < 0) { |
| 56 | bb_error_msg_and_die("Couldn't remove %s", filename); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return status; |
| 61 | } |
| 62 | |