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