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