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 19:35:15 +05:30
|
|
|
#include <fcntl.h>
|
2002-11-03 12:58:38 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-11-03 19:35:15 +05:30
|
|
|
#include <string.h>
|
2002-03-27 23:01:01 +05:30
|
|
|
#include <unistd.h>
|
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
|
|
|
|
|
2001-10-05 09:18:57 +05:30
|
|
|
int bunzip2_main(int argc, char **argv)
|
|
|
|
{
|
2005-08-31 01:56:17 +05:30
|
|
|
char *filename;
|
2004-01-05 17:19:55 +05:30
|
|
|
unsigned long opt;
|
2005-08-31 01:56:17 +05:30
|
|
|
int status, src_fd, dst_fd;
|
2001-11-18 19:50:25 +05:30
|
|
|
|
2004-01-05 17:19:55 +05:30
|
|
|
opt = bb_getopt_ulflags(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];
|
|
|
|
if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
|
2002-03-27 23:01:01 +05:30
|
|
|
/* Open input file */
|
2005-08-31 01:56:17 +05:30
|
|
|
src_fd = bb_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 */
|
|
|
|
if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c')
|
|
|
|
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-06-13 20:24:42 +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;
|
2005-08-31 01:56:17 +05:30
|
|
|
char *extension=filename+strlen(filename)-4;
|
|
|
|
if (strcmp(extension, ".bz2") != 0) {
|
2004-01-05 17:19:55 +05:30
|
|
|
bb_error_msg_and_die("Invalid extension");
|
|
|
|
}
|
2006-06-13 21:39:16 +05:30
|
|
|
xstat(filename, &stat_buf);
|
2005-08-31 01:56:17 +05:30
|
|
|
*extension=0;
|
2006-06-13 20:24:42 +05:30
|
|
|
dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, 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);
|
2005-08-31 01:56:17 +05:30
|
|
|
if(filename) {
|
|
|
|
if (!status) filename[strlen(filename)]='.';
|
|
|
|
if (unlink(filename) < 0) {
|
|
|
|
bb_error_msg_and_die("Couldn't 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
|
|
|
}
|
2005-08-31 01:56:17 +05:30
|
|
|
/* vi:set ts=4: */
|