blob: 2de37d27353a29832ce872805be0797aa4252299 [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>
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
Glenn L McGratha0b37052003-06-22 06:59:34 +000031#define GUNZIP_TO_STDOUT 1
32#define GUNZIP_FORCE 2
33
34extern int uncompress_main(int argc, char **argv)
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000035{
Glenn L McGratha0b37052003-06-22 06:59:34 +000036 int status = EXIT_SUCCESS;
37 unsigned long flags;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000038
Glenn L McGratha0b37052003-06-22 06:59:34 +000039 flags = bb_getopt_ulflags(argc, argv, "cf");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000040
Glenn L McGratha0b37052003-06-22 06:59:34 +000041 while (optind < argc) {
42 const char *compressed_file = argv[optind++];
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000043 const char *delete_path = NULL;
Glenn L McGratha0b37052003-06-22 06:59:34 +000044 char *uncompressed_file = NULL;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000045 int src_fd;
46 int dst_fd;
47
Glenn L McGratha0b37052003-06-22 06:59:34 +000048 if (strcmp(compressed_file, "-") == 0) {
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000049 src_fd = fileno(stdin);
Glenn L McGratha0b37052003-06-22 06:59:34 +000050 flags |= GUNZIP_TO_STDOUT;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000051 } else {
Glenn L McGratha0b37052003-06-22 06:59:34 +000052 src_fd = bb_xopen(compressed_file, O_RDONLY);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000053 }
54
55 /* Check that the input is sane. */
Glenn L McGratha0b37052003-06-22 06:59:34 +000056 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_error_msg_and_die
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000058 ("compressed data not read from terminal. Use -f to force it.");
59 }
60
61 /* Set output filename and number */
Glenn L McGratha0b37052003-06-22 06:59:34 +000062 if (flags & GUNZIP_TO_STDOUT) {
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000063 dst_fd = fileno(stdout);
64 } else {
Glenn L McGratha0b37052003-06-22 06:59:34 +000065 struct stat stat_buf;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000066 char *extension;
67
Glenn L McGratha0b37052003-06-22 06:59:34 +000068 uncompressed_file = bb_xstrdup(compressed_file);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000069
Glenn L McGratha0b37052003-06-22 06:59:34 +000070 extension = strrchr(uncompressed_file, '.');
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000071 if (!extension || (strcmp(extension, ".Z") != 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 bb_error_msg_and_die("Invalid extension");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000073 }
74 *extension = '\0';
75
76 /* Open output file */
Glenn L McGratha0b37052003-06-22 06:59:34 +000077 dst_fd = bb_xopen(uncompressed_file, O_WRONLY | O_CREAT);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000078
79 /* Set permissions on the file */
Glenn L McGratha0b37052003-06-22 06:59:34 +000080 stat(compressed_file, &stat_buf);
81 chmod(uncompressed_file, stat_buf.st_mode);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000082
83 /* If unzip succeeds remove the old file */
Glenn L McGratha0b37052003-06-22 06:59:34 +000084 delete_path = compressed_file;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000085 }
86
87 /* do the decompression, and cleanup */
Glenn L McGratha0b37052003-06-22 06:59:34 +000088 if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 bb_error_msg_and_die("Invalid magic");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000090 }
91
Glenn L McGratha0b37052003-06-22 06:59:34 +000092 status = uncompress(src_fd, dst_fd);
93
94 if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
95 /* Unzip failed, remove the uncomressed file instead of compressed file */
96 delete_path = uncompressed_file;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000097 }
98
99 if (dst_fd != fileno(stdout)) {
100 close(dst_fd);
101 }
102 if (src_fd != fileno(stdin)) {
103 close(src_fd);
104 }
105
106 /* delete_path will be NULL if in test mode or from stdin */
107 if (delete_path && (unlink(delete_path) == -1)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 bb_error_msg_and_die("Couldn't remove %s", delete_path);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +0000109 }
110
Glenn L McGratha0b37052003-06-22 06:59:34 +0000111 free(uncompressed_file);
112 }
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +0000113
114 return status;
115}