2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-03 07:28:38 +00:00
|
|
|
/*
|
2004-04-25 05:11:19 +00:00
|
|
|
* Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
|
2002-11-03 07:28:38 +00:00
|
|
|
* Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
|
|
|
|
*
|
2006-04-17 22:49:30 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2002-11-03 07:28:38 +00:00
|
|
|
*/
|
2001-10-05 03:48:57 +00:00
|
|
|
|
2002-11-03 07:28:38 +00:00
|
|
|
#include "busybox.h"
|
|
|
|
#include "unarchive.h"
|
2001-10-05 03:48:57 +00:00
|
|
|
|
2004-01-05 11:49:55 +00:00
|
|
|
#define BUNZIP2_OPT_STDOUT 1
|
|
|
|
#define BUNZIP2_OPT_FORCE 2
|
|
|
|
|
2001-10-05 03:48:57 +00:00
|
|
|
int bunzip2_main(int argc, char **argv)
|
|
|
|
{
|
2006-10-01 15:55:11 +00:00
|
|
|
USE_DESKTOP(long long) int status;
|
2005-08-30 20:26:17 +00:00
|
|
|
char *filename;
|
2006-10-03 21:00:06 +00:00
|
|
|
unsigned opt;
|
2006-10-01 15:55:11 +00:00
|
|
|
int src_fd, dst_fd;
|
2001-11-18 14:20:25 +00:00
|
|
|
|
2006-10-03 21:00:06 +00:00
|
|
|
opt = getopt32(argc, argv, "cf");
|
2001-11-18 14:20:25 +00:00
|
|
|
|
2002-03-27 17:31:01 +00:00
|
|
|
/* Set input filename and number */
|
2005-08-30 20:26:17 +00:00
|
|
|
filename = argv[optind];
|
2006-12-16 23:49:13 +00:00
|
|
|
if (filename && NOT_LONE_DASH(filename)) {
|
2002-03-27 17:31:01 +00:00
|
|
|
/* Open input file */
|
2006-08-03 15:41:12 +00:00
|
|
|
src_fd = xopen(filename, O_RDONLY);
|
2004-01-05 11:49:55 +00:00
|
|
|
} else {
|
2004-03-27 10:02:48 +00:00
|
|
|
src_fd = STDIN_FILENO;
|
2005-08-30 20:26:17 +00:00
|
|
|
filename = 0;
|
2001-10-05 03:48:57 +00:00
|
|
|
}
|
2005-09-11 01:05:30 +00:00
|
|
|
|
2005-08-30 20:26:17 +00:00
|
|
|
/* if called as bzcat force the stdout flag */
|
2006-10-03 21:00:43 +00:00
|
|
|
if ((opt & BUNZIP2_OPT_STDOUT) || applet_name[2] == 'c')
|
2005-08-30 20:26:17 +00:00
|
|
|
filename = 0;
|
2002-03-27 17:31:01 +00:00
|
|
|
|
|
|
|
/* Check that the input is sane. */
|
2004-01-05 11:49:55 +00:00
|
|
|
if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
|
2006-10-12 20:06:18 +00:00
|
|
|
bb_error_msg_and_die("compressed data not read from terminal, "
|
|
|
|
"use -f to force it");
|
2002-11-03 14:05:15 +00:00
|
|
|
}
|
2001-11-18 14:20:25 +00:00
|
|
|
|
2005-08-30 20:26:17 +00:00
|
|
|
if (filename) {
|
2006-06-13 14:54:42 +00:00
|
|
|
struct stat stat_buf;
|
2006-09-03 14:23:29 +00:00
|
|
|
/* extension = filename+strlen(filename)-4 is buggy:
|
|
|
|
* strlen may be less than 4 */
|
|
|
|
char *extension = strrchr(filename, '.');
|
|
|
|
if (!extension || strcmp(extension, ".bz2") != 0) {
|
2006-09-27 23:31:08 +00:00
|
|
|
bb_error_msg_and_die("invalid extension");
|
2004-01-05 11:49:55 +00:00
|
|
|
}
|
2006-06-13 16:09:16 +00:00
|
|
|
xstat(filename, &stat_buf);
|
2006-09-03 14:23:29 +00:00
|
|
|
*extension = '\0';
|
|
|
|
dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
stat_buf.st_mode);
|
2005-08-30 20:26:17 +00:00
|
|
|
} else dst_fd = STDOUT_FILENO;
|
2003-10-29 03:37:54 +00:00
|
|
|
status = uncompressStream(src_fd, dst_fd);
|
2006-09-27 23:31:08 +00:00
|
|
|
if (filename) {
|
2006-10-01 15:55:11 +00:00
|
|
|
if (status >= 0) filename[strlen(filename)] = '.';
|
2005-08-30 20:26:17 +00:00
|
|
|
if (unlink(filename) < 0) {
|
2006-09-27 23:31:08 +00:00
|
|
|
bb_error_msg_and_die("cannot remove %s", filename);
|
2002-11-03 14:05:15 +00:00
|
|
|
}
|
2002-03-27 17:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2001-10-05 03:48:57 +00:00
|
|
|
}
|