blob: b282fe811c7f3a1468c79b00014b7ac77e309f04 [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
8#include <stdlib.h>
9#include <string.h>
10#include <unistd.h>
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000011#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000015#include "busybox.h"
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000016#include "unarchive.h"
17
Glenn L McGratha0b37052003-06-22 06:59:34 +000018#define GUNZIP_TO_STDOUT 1
19#define GUNZIP_FORCE 2
20
Rob Landleydfba7412006-03-06 20:47:33 +000021int uncompress_main(int argc, char **argv)
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000022{
Glenn L McGratha0b37052003-06-22 06:59:34 +000023 int status = EXIT_SUCCESS;
24 unsigned long flags;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000025
Glenn L McGratha0b37052003-06-22 06:59:34 +000026 flags = bb_getopt_ulflags(argc, argv, "cf");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000027
Glenn L McGratha0b37052003-06-22 06:59:34 +000028 while (optind < argc) {
29 const char *compressed_file = argv[optind++];
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000030 const char *delete_path = NULL;
Glenn L McGratha0b37052003-06-22 06:59:34 +000031 char *uncompressed_file = NULL;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000032 int src_fd;
33 int dst_fd;
34
Glenn L McGratha0b37052003-06-22 06:59:34 +000035 if (strcmp(compressed_file, "-") == 0) {
Eric Andersen70060d22004-03-27 10:02:48 +000036 src_fd = STDIN_FILENO;
Glenn L McGratha0b37052003-06-22 06:59:34 +000037 flags |= GUNZIP_TO_STDOUT;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000038 } else {
Glenn L McGratha0b37052003-06-22 06:59:34 +000039 src_fd = bb_xopen(compressed_file, O_RDONLY);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000040 }
41
42 /* Check that the input is sane. */
Glenn L McGratha0b37052003-06-22 06:59:34 +000043 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 bb_error_msg_and_die
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000045 ("compressed data not read from terminal. Use -f to force it.");
46 }
47
48 /* Set output filename and number */
Glenn L McGratha0b37052003-06-22 06:59:34 +000049 if (flags & GUNZIP_TO_STDOUT) {
Eric Andersen70060d22004-03-27 10:02:48 +000050 dst_fd = STDOUT_FILENO;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000051 } else {
Glenn L McGratha0b37052003-06-22 06:59:34 +000052 struct stat stat_buf;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000053 char *extension;
54
Glenn L McGratha0b37052003-06-22 06:59:34 +000055 uncompressed_file = bb_xstrdup(compressed_file);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000056
Glenn L McGratha0b37052003-06-22 06:59:34 +000057 extension = strrchr(uncompressed_file, '.');
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000058 if (!extension || (strcmp(extension, ".Z") != 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 bb_error_msg_and_die("Invalid extension");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000060 }
61 *extension = '\0';
62
63 /* Open output file */
Rob Landleyc4b67392006-06-13 16:09:16 +000064 xstat(compressed_file, &stat_buf);
65 dst_fd = bb_xopen3(uncompressed_file, O_WRONLY | O_CREAT,
66 stat_buf.st_mode);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000067
68 /* If unzip succeeds remove the old file */
Glenn L McGratha0b37052003-06-22 06:59:34 +000069 delete_path = compressed_file;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000070 }
71
72 /* do the decompression, and cleanup */
Glenn L McGratha0b37052003-06-22 06:59:34 +000073 if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_error_msg_and_die("Invalid magic");
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000075 }
76
Glenn L McGratha0b37052003-06-22 06:59:34 +000077 status = uncompress(src_fd, dst_fd);
78
79 if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
80 /* Unzip failed, remove the uncomressed file instead of compressed file */
81 delete_path = uncompressed_file;
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000082 }
83
Eric Andersen70060d22004-03-27 10:02:48 +000084 if (dst_fd != STDOUT_FILENO) {
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000085 close(dst_fd);
86 }
Eric Andersen70060d22004-03-27 10:02:48 +000087 if (src_fd != STDIN_FILENO) {
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000088 close(src_fd);
89 }
90
91 /* delete_path will be NULL if in test mode or from stdin */
92 if (delete_path && (unlink(delete_path) == -1)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 bb_error_msg_and_die("Couldn't remove %s", delete_path);
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000094 }
95
Glenn L McGratha0b37052003-06-22 06:59:34 +000096 free(uncompressed_file);
97 }
Glenn L McGrathbf1cc8b2002-11-01 23:38:54 +000098
99 return status;
100}