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 | |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 11 | #include <sys/types.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <fcntl.h> |
| 14 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 16 | #include "unarchive.h" |
| 17 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 18 | #define GUNZIP_TO_STDOUT 1 |
| 19 | #define GUNZIP_FORCE 2 |
| 20 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 21 | int uncompress_main(int argc, char **argv) |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 22 | { |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 23 | int status = EXIT_SUCCESS; |
| 24 | unsigned long flags; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 25 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 26 | flags = bb_getopt_ulflags(argc, argv, "cf"); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 27 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 28 | while (optind < argc) { |
| 29 | const char *compressed_file = argv[optind++]; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 30 | const char *delete_path = NULL; |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 31 | char *uncompressed_file = NULL; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 32 | int src_fd; |
| 33 | int dst_fd; |
| 34 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 35 | if (strcmp(compressed_file, "-") == 0) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 36 | src_fd = STDIN_FILENO; |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 37 | flags |= GUNZIP_TO_STDOUT; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 38 | } else { |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 39 | src_fd = bb_xopen(compressed_file, O_RDONLY); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* Check that the input is sane. */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 43 | if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | bb_error_msg_and_die |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 45 | ("compressed data not read from terminal. Use -f to force it."); |
| 46 | } |
| 47 | |
| 48 | /* Set output filename and number */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 49 | if (flags & GUNZIP_TO_STDOUT) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 50 | dst_fd = STDOUT_FILENO; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 51 | } else { |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 52 | struct stat stat_buf; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 53 | char *extension; |
| 54 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 55 | uncompressed_file = bb_xstrdup(compressed_file); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 56 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 57 | extension = strrchr(uncompressed_file, '.'); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 58 | if (!extension || (strcmp(extension, ".Z") != 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | bb_error_msg_and_die("Invalid extension"); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 60 | } |
| 61 | *extension = '\0'; |
| 62 | |
| 63 | /* Open output file */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 64 | dst_fd = bb_xopen(uncompressed_file, O_WRONLY | O_CREAT); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 65 | |
| 66 | /* Set permissions on the file */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 67 | stat(compressed_file, &stat_buf); |
| 68 | chmod(uncompressed_file, stat_buf.st_mode); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 69 | |
| 70 | /* If unzip succeeds remove the old file */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 71 | delete_path = compressed_file; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* do the decompression, and cleanup */ |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 75 | if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | bb_error_msg_and_die("Invalid magic"); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 79 | status = uncompress(src_fd, dst_fd); |
| 80 | |
| 81 | if ((status != EXIT_SUCCESS) && (uncompressed_file)) { |
| 82 | /* Unzip failed, remove the uncomressed file instead of compressed file */ |
| 83 | delete_path = uncompressed_file; |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 86 | if (dst_fd != STDOUT_FILENO) { |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 87 | close(dst_fd); |
| 88 | } |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 89 | if (src_fd != STDIN_FILENO) { |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 90 | close(src_fd); |
| 91 | } |
| 92 | |
| 93 | /* delete_path will be NULL if in test mode or from stdin */ |
| 94 | if (delete_path && (unlink(delete_path) == -1)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | bb_error_msg_and_die("Couldn't remove %s", delete_path); |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Glenn L McGrath | a0b3705 | 2003-06-22 06:59:34 +0000 | [diff] [blame] | 98 | free(uncompressed_file); |
| 99 | } |
Glenn L McGrath | bf1cc8b | 2002-11-01 23:38:54 +0000 | [diff] [blame] | 100 | |
| 101 | return status; |
| 102 | } |