2002-11-02 05:08:54 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Uncompress applet for busybox (c) 2002 Glenn McGrath
|
|
|
|
*
|
2006-04-03 22:09:31 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2002-11-02 05:08:54 +05:30
|
|
|
*/
|
|
|
|
|
2006-04-03 22:09:31 +05:30
|
|
|
#include "busybox.h"
|
2002-11-02 05:08:54 +05:30
|
|
|
#include "unarchive.h"
|
|
|
|
|
2003-06-22 12:29:34 +05:30
|
|
|
#define GUNZIP_TO_STDOUT 1
|
|
|
|
#define GUNZIP_FORCE 2
|
|
|
|
|
2006-03-07 02:17:33 +05:30
|
|
|
int uncompress_main(int argc, char **argv)
|
2002-11-02 05:08:54 +05:30
|
|
|
{
|
2003-06-22 12:29:34 +05:30
|
|
|
int status = EXIT_SUCCESS;
|
|
|
|
unsigned long flags;
|
2002-11-02 05:08:54 +05:30
|
|
|
|
2003-06-22 12:29:34 +05:30
|
|
|
flags = bb_getopt_ulflags(argc, argv, "cf");
|
|
|
|
|
|
|
|
while (optind < argc) {
|
|
|
|
const char *compressed_file = argv[optind++];
|
2002-11-02 05:08:54 +05:30
|
|
|
const char *delete_path = NULL;
|
2003-06-22 12:29:34 +05:30
|
|
|
char *uncompressed_file = NULL;
|
2002-11-02 05:08:54 +05:30
|
|
|
int src_fd;
|
|
|
|
int dst_fd;
|
|
|
|
|
2003-06-22 12:29:34 +05:30
|
|
|
if (strcmp(compressed_file, "-") == 0) {
|
2004-03-27 15:32:48 +05:30
|
|
|
src_fd = STDIN_FILENO;
|
2003-06-22 12:29:34 +05:30
|
|
|
flags |= GUNZIP_TO_STDOUT;
|
2002-11-02 05:08:54 +05:30
|
|
|
} else {
|
2006-08-03 21:11:12 +05:30
|
|
|
src_fd = xopen(compressed_file, O_RDONLY);
|
2002-11-02 05:08:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the input is sane. */
|
2003-06-22 12:29:34 +05:30
|
|
|
if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die
|
2002-11-02 05:08:54 +05:30
|
|
|
("compressed data not read from terminal. Use -f to force it.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set output filename and number */
|
2003-06-22 12:29:34 +05:30
|
|
|
if (flags & GUNZIP_TO_STDOUT) {
|
2004-03-27 15:32:48 +05:30
|
|
|
dst_fd = STDOUT_FILENO;
|
2002-11-02 05:08:54 +05:30
|
|
|
} else {
|
2003-06-22 12:29:34 +05:30
|
|
|
struct stat stat_buf;
|
2002-11-02 05:08:54 +05:30
|
|
|
char *extension;
|
|
|
|
|
2006-08-03 21:11:12 +05:30
|
|
|
uncompressed_file = xstrdup(compressed_file);
|
2002-11-02 05:08:54 +05:30
|
|
|
|
2003-06-22 12:29:34 +05:30
|
|
|
extension = strrchr(uncompressed_file, '.');
|
2002-11-02 05:08:54 +05:30
|
|
|
if (!extension || (strcmp(extension, ".Z") != 0)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("Invalid extension");
|
2002-11-02 05:08:54 +05:30
|
|
|
}
|
|
|
|
*extension = '\0';
|
|
|
|
|
|
|
|
/* Open output file */
|
2006-06-13 21:39:16 +05:30
|
|
|
xstat(compressed_file, &stat_buf);
|
2006-09-03 19:53:29 +05:30
|
|
|
dst_fd = xopen3(uncompressed_file,
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC,
|
2006-06-13 21:39:16 +05:30
|
|
|
stat_buf.st_mode);
|
2002-11-02 05:08:54 +05:30
|
|
|
|
|
|
|
/* If unzip succeeds remove the old file */
|
2003-06-22 12:29:34 +05:30
|
|
|
delete_path = compressed_file;
|
2002-11-02 05:08:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* do the decompression, and cleanup */
|
2006-07-16 13:44:35 +05:30
|
|
|
if ((xread_char(src_fd) != 0x1f) || (xread_char(src_fd) != 0x9d)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("Invalid magic");
|
2002-11-02 05:08:54 +05:30
|
|
|
}
|
|
|
|
|
2003-06-22 12:29:34 +05:30
|
|
|
status = uncompress(src_fd, dst_fd);
|
|
|
|
|
|
|
|
if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
|
|
|
|
/* Unzip failed, remove the uncomressed file instead of compressed file */
|
|
|
|
delete_path = uncompressed_file;
|
2002-11-02 05:08:54 +05:30
|
|
|
}
|
|
|
|
|
2004-03-27 15:32:48 +05:30
|
|
|
if (dst_fd != STDOUT_FILENO) {
|
2002-11-02 05:08:54 +05:30
|
|
|
close(dst_fd);
|
|
|
|
}
|
2004-03-27 15:32:48 +05:30
|
|
|
if (src_fd != STDIN_FILENO) {
|
2002-11-02 05:08:54 +05:30
|
|
|
close(src_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete_path will be NULL if in test mode or from stdin */
|
|
|
|
if (delete_path && (unlink(delete_path) == -1)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("Couldn't remove %s", delete_path);
|
2002-11-02 05:08:54 +05:30
|
|
|
}
|
|
|
|
|
2003-06-22 12:29:34 +05:30
|
|
|
free(uncompressed_file);
|
|
|
|
}
|
2002-11-02 05:08:54 +05:30
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|