blob: b16c353ecfcb502b146f5539a368ac6fb2fc78dd [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 *
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +00006 */
7
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00008#include "busybox.h"
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +00009#include "unarchive.h"
10
Glenn L McGratha0b37052003-06-22 06:59:34 +000011#define GUNZIP_TO_STDOUT 1
12#define GUNZIP_FORCE 2
13
Denis Vlasenko06af2162007-02-03 17:28:39 +000014int uncompress_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000015int uncompress_main(int argc, char **argv)
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000016{
Glenn L McGratha0b37052003-06-22 06:59:34 +000017 int status = EXIT_SUCCESS;
18 unsigned long flags;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000019
Denis Vlasenko67b23e62006-10-03 21:00:06 +000020 flags = getopt32(argc, argv, "cf");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000021
Glenn L McGratha0b37052003-06-22 06:59:34 +000022 while (optind < argc) {
Rob Landley20cc6d52006-09-12 21:42:17 +000023 char *compressed_file = argv[optind++];
24 char *delete_path = NULL;
Glenn L McGratha0b37052003-06-22 06:59:34 +000025 char *uncompressed_file = NULL;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000026 int src_fd;
27 int dst_fd;
28
Denis Vlasenko9f739442006-12-16 23:49:13 +000029 if (LONE_DASH(compressed_file)) {
Eric Andersen70060d22004-03-27 10:02:48 +000030 src_fd = STDIN_FILENO;
Glenn L McGratha0b37052003-06-22 06:59:34 +000031 flags |= GUNZIP_TO_STDOUT;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000032 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +000033 src_fd = xopen(compressed_file, O_RDONLY);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000034 }
35
36 /* Check that the input is sane. */
Glenn L McGratha0b37052003-06-22 06:59:34 +000037 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 bb_error_msg_and_die
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000039 ("compressed data not read from terminal. Use -f to force it.");
40 }
41
42 /* Set output filename and number */
Glenn L McGratha0b37052003-06-22 06:59:34 +000043 if (flags & GUNZIP_TO_STDOUT) {
Eric Andersen70060d22004-03-27 10:02:48 +000044 dst_fd = STDOUT_FILENO;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000045 } else {
Glenn L McGratha0b37052003-06-22 06:59:34 +000046 struct stat stat_buf;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000047 char *extension;
48
Rob Landleyd921b2e2006-08-03 15:41:12 +000049 uncompressed_file = xstrdup(compressed_file);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000050
Glenn L McGratha0b37052003-06-22 06:59:34 +000051 extension = strrchr(uncompressed_file, '.');
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000052 if (!extension || (strcmp(extension, ".Z") != 0)) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000053 bb_error_msg_and_die("invalid extension");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000054 }
55 *extension = '\0';
56
57 /* Open output file */
Rob Landleyc4b67392006-06-13 16:09:16 +000058 xstat(compressed_file, &stat_buf);
Denis Vlasenko22dca232006-09-03 14:23:29 +000059 dst_fd = xopen3(uncompressed_file,
60 O_WRONLY | O_CREAT | O_TRUNC,
Rob Landleyc4b67392006-06-13 16:09:16 +000061 stat_buf.st_mode);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000062
63 /* If unzip succeeds remove the old file */
Glenn L McGratha0b37052003-06-22 06:59:34 +000064 delete_path = compressed_file;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000065 }
66
67 /* do the decompression, and cleanup */
Rob Landley53437472006-07-16 08:14:35 +000068 if ((xread_char(src_fd) != 0x1f) || (xread_char(src_fd) != 0x9d)) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000069 bb_error_msg_and_die("invalid magic");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000070 }
71
Glenn L McGratha0b37052003-06-22 06:59:34 +000072 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 McGrathbf1cc8b2002-11-01 23:38:54 +000077 }
78
Eric Andersen70060d22004-03-27 10:02:48 +000079 if (dst_fd != STDOUT_FILENO) {
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000080 close(dst_fd);
81 }
Eric Andersen70060d22004-03-27 10:02:48 +000082 if (src_fd != STDIN_FILENO) {
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000083 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 Vlasenkoa9595882006-09-29 21:30:43 +000088 bb_error_msg_and_die("cannot remove %s", delete_path);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000089 }
90
Glenn L McGratha0b37052003-06-22 06:59:34 +000091 free(uncompressed_file);
92 }
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000093
94 return status;
95}