Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Uncompress applet for busybox (c) 2002 Glenn McGrath |
| 4 | * |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 8 | #include "busybox.h" |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 9 | #include "unarchive.h" |
| 10 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 11 | #define GUNZIP_TO_STDOUT 1 |
| 12 | #define GUNZIP_FORCE 2 |
| 13 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame^] | 14 | int uncompress_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 15 | int uncompress_main(int argc, char **argv) |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 16 | { |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 17 | int status = EXIT_SUCCESS; |
| 18 | unsigned long flags; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 20 | flags = getopt32(argc, argv, "cf"); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 21 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 22 | while (optind < argc) { |
Rob Landley | 20cc6d5 | 2006-09-12 21:42:17 +0000 | [diff] [blame] | 23 | char *compressed_file = argv[optind++]; |
| 24 | char *delete_path = NULL; |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 25 | char *uncompressed_file = NULL; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 26 | int src_fd; |
| 27 | int dst_fd; |
| 28 | |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 29 | if (LONE_DASH(compressed_file)) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 30 | src_fd = STDIN_FILENO; |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 31 | flags |= GUNZIP_TO_STDOUT; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 32 | } else { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 33 | src_fd = xopen(compressed_file, O_RDONLY); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* Check that the input is sane. */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 37 | if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | bb_error_msg_and_die |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 39 | ("compressed data not read from terminal. Use -f to force it."); |
| 40 | } |
| 41 | |
| 42 | /* Set output filename and number */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 43 | if (flags & GUNZIP_TO_STDOUT) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 44 | dst_fd = STDOUT_FILENO; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 45 | } else { |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 46 | struct stat stat_buf; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 47 | char *extension; |
| 48 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 49 | uncompressed_file = xstrdup(compressed_file); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 51 | extension = strrchr(uncompressed_file, '.'); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 52 | if (!extension || (strcmp(extension, ".Z") != 0)) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 53 | bb_error_msg_and_die("invalid extension"); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 54 | } |
| 55 | *extension = '\0'; |
| 56 | |
| 57 | /* Open output file */ |
Rob Landley | c4b6739 | 2006-06-13 16:09:16 +0000 | [diff] [blame] | 58 | xstat(compressed_file, &stat_buf); |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 59 | dst_fd = xopen3(uncompressed_file, |
| 60 | O_WRONLY | O_CREAT | O_TRUNC, |
Rob Landley | c4b6739 | 2006-06-13 16:09:16 +0000 | [diff] [blame] | 61 | stat_buf.st_mode); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 62 | |
| 63 | /* If unzip succeeds remove the old file */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 64 | delete_path = compressed_file; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* do the decompression, and cleanup */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 68 | if ((xread_char(src_fd) != 0x1f) || (xread_char(src_fd) != 0x9d)) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 69 | bb_error_msg_and_die("invalid magic"); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 72 | status = uncompress(src_fd, dst_fd); |
| 73 | |
| 74 | if ((status != EXIT_SUCCESS) && (uncompressed_file)) { |
| 75 | /* Unzip failed, remove the uncomressed file instead of compressed file */ |
| 76 | delete_path = uncompressed_file; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 79 | if (dst_fd != STDOUT_FILENO) { |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 80 | close(dst_fd); |
| 81 | } |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 82 | if (src_fd != STDIN_FILENO) { |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 83 | close(src_fd); |
| 84 | } |
| 85 | |
| 86 | /* delete_path will be NULL if in test mode or from stdin */ |
| 87 | if (delete_path && (unlink(delete_path) == -1)) { |
Denis Vlasenko | a959588 | 2006-09-29 21:30:43 +0000 | [diff] [blame] | 88 | bb_error_msg_and_die("cannot remove %s", delete_path); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 91 | free(uncompressed_file); |
| 92 | } |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 93 | |
| 94 | return status; |
| 95 | } |