2002-11-01 23:38:54 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Uncompress applet for busybox (c) 2002 Glenn McGrath
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "unarchive.h"
|
|
|
|
|
2003-06-22 06:59:34 +00:00
|
|
|
#define GUNZIP_TO_STDOUT 1
|
|
|
|
#define GUNZIP_FORCE 2
|
|
|
|
|
|
|
|
extern 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
|
|
|
|
2003-06-22 06:59:34 +00:00
|
|
|
flags = bb_getopt_ulflags(argc, argv, "cf");
|
|
|
|
|
|
|
|
while (optind < argc) {
|
|
|
|
const char *compressed_file = argv[optind++];
|
2002-11-01 23:38:54 +00:00
|
|
|
const 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;
|
|
|
|
|
2003-06-22 06:59:34 +00:00
|
|
|
if (strcmp(compressed_file, "-") == 0) {
|
2002-11-01 23:38:54 +00:00
|
|
|
src_fd = fileno(stdin);
|
2003-06-22 06:59:34 +00:00
|
|
|
flags |= GUNZIP_TO_STDOUT;
|
2002-11-01 23:38:54 +00:00
|
|
|
} else {
|
2003-06-22 06:59:34 +00:00
|
|
|
src_fd = bb_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) {
|
2002-11-01 23:38:54 +00:00
|
|
|
dst_fd = fileno(stdout);
|
|
|
|
} else {
|
2003-06-22 06:59:34 +00:00
|
|
|
struct stat stat_buf;
|
2002-11-01 23:38:54 +00:00
|
|
|
char *extension;
|
|
|
|
|
2003-06-22 06:59:34 +00:00
|
|
|
uncompressed_file = bb_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)) {
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_error_msg_and_die("Invalid extension");
|
2002-11-01 23:38:54 +00:00
|
|
|
}
|
|
|
|
*extension = '\0';
|
|
|
|
|
|
|
|
/* Open output file */
|
2003-06-22 06:59:34 +00:00
|
|
|
dst_fd = bb_xopen(uncompressed_file, O_WRONLY | O_CREAT);
|
2002-11-01 23:38:54 +00:00
|
|
|
|
|
|
|
/* Set permissions on the file */
|
2003-06-22 06:59:34 +00:00
|
|
|
stat(compressed_file, &stat_buf);
|
|
|
|
chmod(uncompressed_file, 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 */
|
2003-06-22 06:59:34 +00:00
|
|
|
if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
|
2003-03-19 09:13:01 +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
|
|
|
}
|
|
|
|
|
|
|
|
if (dst_fd != fileno(stdout)) {
|
|
|
|
close(dst_fd);
|
|
|
|
}
|
|
|
|
if (src_fd != fileno(stdin)) {
|
|
|
|
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 09:13:01 +00:00
|
|
|
bb_error_msg_and_die("Couldn't 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;
|
|
|
|
}
|