"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 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 11 | /* Why our g[un]zip/bunzip2 are so ugly compared to this beauty? */ |
| 12 | |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
| 14 | #include "unarchive.h" |
| 15 | |
| 16 | #define UNLZMA_OPT_STDOUT 1 |
| 17 | |
| 18 | int unlzma_main(int argc, char **argv) |
| 19 | { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 20 | USE_DESKTOP(long long) int status; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 21 | char *filename; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 22 | unsigned opt; |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 23 | int src_fd, dst_fd; |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 25 | opt = getopt32(argc, argv, "c"); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 26 | |
| 27 | /* Set input filename and number */ |
| 28 | filename = argv[optind]; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 29 | if (filename && NOT_LONE_DASH(filename)) { |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 30 | /* Open input file */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 31 | src_fd = xopen(filename, O_RDONLY); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 32 | } else { |
| 33 | src_fd = STDIN_FILENO; |
| 34 | filename = 0; |
| 35 | } |
| 36 | |
| 37 | /* if called as lzmacat force the stdout flag */ |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 38 | if ((opt & UNLZMA_OPT_STDOUT) || applet_name[4] == 'c') |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 39 | filename = 0; |
| 40 | |
| 41 | if (filename) { |
Rob Landley | 9a202c9 | 2006-06-13 14:54:42 +0000 | [diff] [blame] | 42 | struct stat stat_buf; |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 43 | /* bug: char *extension = filename + strlen(filename) - 5; */ |
| 44 | char *extension = strrchr(filename, '.'); |
| 45 | if (!extension || strcmp(extension, ".lzma") != 0) { |
Denis Vlasenko | a959588 | 2006-09-29 21:30:43 +0000 | [diff] [blame] | 46 | bb_error_msg_and_die("invalid extension"); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 47 | } |
Rob Landley | c4b6739 | 2006-06-13 16:09:16 +0000 | [diff] [blame] | 48 | xstat(filename, &stat_buf); |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 49 | *extension = '\0'; |
| 50 | dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC, |
| 51 | stat_buf.st_mode); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 52 | } else |
| 53 | dst_fd = STDOUT_FILENO; |
| 54 | status = unlzma(src_fd, dst_fd); |
| 55 | if (filename) { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 56 | if (status >= 0) /* if success delete src, else delete dst */ |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 57 | filename[strlen(filename)] = '.'; |
| 58 | if (unlink(filename) < 0) { |
Denis Vlasenko | a959588 | 2006-09-29 21:30:43 +0000 | [diff] [blame] | 59 | bb_error_msg_and_die("cannot remove %s", filename); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 63 | return (status < 0); |
Rob Landley | c1d6990 | 2006-01-20 18:28:50 +0000 | [diff] [blame] | 64 | } |