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 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | #include <getopt.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | |
| 28 | #include "libbb.h" |
| 29 | #include "unarchive.h" |
| 30 | |
| 31 | int uncompress_main(int argc, char **argv) |
| 32 | { |
| 33 | const char gunzip_to_stdout = 1; |
| 34 | const char gunzip_force = 2; |
| 35 | char status = EXIT_SUCCESS; |
| 36 | char flags = 0; |
| 37 | int opt; |
| 38 | |
| 39 | while ((opt = getopt(argc, argv, "cfh")) != -1) { |
| 40 | switch (opt) { |
| 41 | case 'c': |
| 42 | flags |= gunzip_to_stdout; |
| 43 | break; |
| 44 | case 'f': |
| 45 | flags |= gunzip_force; |
| 46 | break; |
| 47 | default: |
| 48 | show_usage(); /* exit's inside usage */ |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | do { |
| 53 | struct stat stat_buf; |
| 54 | const char *old_path = argv[optind]; |
| 55 | const char *delete_path = NULL; |
| 56 | char *new_path = NULL; |
| 57 | int src_fd; |
| 58 | int dst_fd; |
| 59 | |
| 60 | optind++; |
| 61 | |
| 62 | if (old_path == NULL || strcmp(old_path, "-") == 0) { |
| 63 | src_fd = fileno(stdin); |
| 64 | flags |= gunzip_to_stdout; |
| 65 | } else { |
| 66 | src_fd = xopen(old_path, O_RDONLY); |
| 67 | |
| 68 | /* Get the time stamp on the input file. */ |
| 69 | if (stat(old_path, &stat_buf) < 0) { |
| 70 | error_msg_and_die("Couldn't stat file %s", old_path); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* Check that the input is sane. */ |
| 75 | if (isatty(src_fd) && ((flags & gunzip_force) == 0)) { |
| 76 | error_msg_and_die |
| 77 | ("compressed data not read from terminal. Use -f to force it."); |
| 78 | } |
| 79 | |
| 80 | /* Set output filename and number */ |
| 81 | if (flags & gunzip_to_stdout) { |
| 82 | dst_fd = fileno(stdout); |
| 83 | } else { |
| 84 | char *extension; |
| 85 | |
| 86 | new_path = xstrdup(old_path); |
| 87 | |
| 88 | extension = strrchr(new_path, '.'); |
| 89 | if (!extension || (strcmp(extension, ".Z") != 0)) { |
| 90 | error_msg_and_die("Invalid extension"); |
| 91 | } |
| 92 | *extension = '\0'; |
| 93 | |
| 94 | /* Open output file */ |
| 95 | dst_fd = xopen(new_path, O_WRONLY | O_CREAT); |
| 96 | |
| 97 | /* Set permissions on the file */ |
| 98 | chmod(new_path, stat_buf.st_mode); |
| 99 | |
| 100 | /* If unzip succeeds remove the old file */ |
| 101 | delete_path = old_path; |
| 102 | } |
| 103 | |
| 104 | /* do the decompression, and cleanup */ |
| 105 | if ((xread_char(src_fd) == 0x1f) && (xread_char(src_fd) == 0x9d)) { |
| 106 | status = uncompress(src_fd, dst_fd); |
| 107 | } else { |
| 108 | error_msg_and_die("Invalid magic"); |
| 109 | } |
| 110 | |
| 111 | if ((status != EXIT_SUCCESS) && (new_path)) { |
| 112 | /* Unzip failed, remove new path instead of old path */ |
| 113 | delete_path = new_path; |
| 114 | } |
| 115 | |
| 116 | if (dst_fd != fileno(stdout)) { |
| 117 | close(dst_fd); |
| 118 | } |
| 119 | if (src_fd != fileno(stdin)) { |
| 120 | close(src_fd); |
| 121 | } |
| 122 | |
| 123 | /* delete_path will be NULL if in test mode or from stdin */ |
| 124 | if (delete_path && (unlink(delete_path) == -1)) { |
| 125 | error_msg_and_die("Couldn't remove %s", delete_path); |
| 126 | } |
| 127 | |
| 128 | free(new_path); |
| 129 | |
| 130 | } while (optind < argc); |
| 131 | |
| 132 | return status; |
| 133 | } |