blob: 3bd87093e9a2b411adae1efa2bcf6dfeebeedc98 [file] [log] [blame]
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +00001/* 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>
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000023#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26
27#include "libbb.h"
28#include "unarchive.h"
29
Glenn L McGratha0b37052003-06-22 06:59:34 +000030#define GUNZIP_TO_STDOUT 1
31#define GUNZIP_FORCE 2
32
33extern int uncompress_main(int argc, char **argv)
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000034{
Glenn L McGratha0b37052003-06-22 06:59:34 +000035 int status = EXIT_SUCCESS;
36 unsigned long flags;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000037
Glenn L McGratha0b37052003-06-22 06:59:34 +000038 flags = bb_getopt_ulflags(argc, argv, "cf");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000039
Glenn L McGratha0b37052003-06-22 06:59:34 +000040 while (optind < argc) {
41 const char *compressed_file = argv[optind++];
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000042 const char *delete_path = NULL;
Glenn L McGratha0b37052003-06-22 06:59:34 +000043 char *uncompressed_file = NULL;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000044 int src_fd;
45 int dst_fd;
46
Glenn L McGratha0b37052003-06-22 06:59:34 +000047 if (strcmp(compressed_file, "-") == 0) {
Eric Andersen70060d22004-03-27 10:02:48 +000048 src_fd = STDIN_FILENO;
Glenn L McGratha0b37052003-06-22 06:59:34 +000049 flags |= GUNZIP_TO_STDOUT;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000050 } else {
Glenn L McGratha0b37052003-06-22 06:59:34 +000051 src_fd = bb_xopen(compressed_file, O_RDONLY);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000052 }
53
54 /* Check that the input is sane. */
Glenn L McGratha0b37052003-06-22 06:59:34 +000055 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_error_msg_and_die
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000057 ("compressed data not read from terminal. Use -f to force it.");
58 }
59
60 /* Set output filename and number */
Glenn L McGratha0b37052003-06-22 06:59:34 +000061 if (flags & GUNZIP_TO_STDOUT) {
Eric Andersen70060d22004-03-27 10:02:48 +000062 dst_fd = STDOUT_FILENO;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000063 } else {
Glenn L McGratha0b37052003-06-22 06:59:34 +000064 struct stat stat_buf;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000065 char *extension;
66
Glenn L McGratha0b37052003-06-22 06:59:34 +000067 uncompressed_file = bb_xstrdup(compressed_file);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000068
Glenn L McGratha0b37052003-06-22 06:59:34 +000069 extension = strrchr(uncompressed_file, '.');
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000070 if (!extension || (strcmp(extension, ".Z") != 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 bb_error_msg_and_die("Invalid extension");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000072 }
73 *extension = '\0';
74
75 /* Open output file */
Glenn L McGratha0b37052003-06-22 06:59:34 +000076 dst_fd = bb_xopen(uncompressed_file, O_WRONLY | O_CREAT);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000077
78 /* Set permissions on the file */
Glenn L McGratha0b37052003-06-22 06:59:34 +000079 stat(compressed_file, &stat_buf);
80 chmod(uncompressed_file, stat_buf.st_mode);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000081
82 /* If unzip succeeds remove the old file */
Glenn L McGratha0b37052003-06-22 06:59:34 +000083 delete_path = compressed_file;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000084 }
85
86 /* do the decompression, and cleanup */
Glenn L McGratha0b37052003-06-22 06:59:34 +000087 if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 bb_error_msg_and_die("Invalid magic");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000089 }
90
Glenn L McGratha0b37052003-06-22 06:59:34 +000091 status = uncompress(src_fd, dst_fd);
92
93 if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
94 /* Unzip failed, remove the uncomressed file instead of compressed file */
95 delete_path = uncompressed_file;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000096 }
97
Eric Andersen70060d22004-03-27 10:02:48 +000098 if (dst_fd != STDOUT_FILENO) {
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000099 close(dst_fd);
100 }
Eric Andersen70060d22004-03-27 10:02:48 +0000101 if (src_fd != STDIN_FILENO) {
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +0000102 close(src_fd);
103 }
104
105 /* delete_path will be NULL if in test mode or from stdin */
106 if (delete_path && (unlink(delete_path) == -1)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 bb_error_msg_and_die("Couldn't remove %s", delete_path);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +0000108 }
109
Glenn L McGratha0b37052003-06-22 06:59:34 +0000110 free(uncompressed_file);
111 }
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +0000112
113 return status;
114}