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