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>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-10-05 03:48:57 +00:00
|
|
|
|
2002-11-03 14:05:15 +00:00
|
|
|
#include <fcntl.h>
|
2001-11-18 14:20:25 +00:00
|
|
|
#include <getopt.h>
|
2002-11-03 07:28:38 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-11-03 14:05:15 +00:00
|
|
|
#include <string.h>
|
2002-03-27 17:31:01 +00:00
|
|
|
#include <unistd.h>
|
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)
|
|
|
|
{
|
2004-01-05 11:49:55 +00:00
|
|
|
char *compressed_name;
|
|
|
|
char *save_name;
|
|
|
|
unsigned long opt;
|
2002-03-27 17:46:44 +00:00
|
|
|
int status;
|
2002-11-03 14:05:15 +00:00
|
|
|
int src_fd;
|
|
|
|
int dst_fd;
|
2001-11-18 14:20:25 +00:00
|
|
|
|
2004-01-05 11:49:55 +00:00
|
|
|
opt = bb_getopt_ulflags(argc, argv, "cf");
|
2001-11-18 14:20:25 +00:00
|
|
|
|
2004-01-05 11:49:55 +00:00
|
|
|
/* if called as bzcat force the stdout flag */
|
|
|
|
if (bb_applet_name[2] == 'c') {
|
|
|
|
opt |= BUNZIP2_OPT_STDOUT;
|
2001-11-18 14:20:25 +00:00
|
|
|
}
|
|
|
|
|
2002-03-27 17:31:01 +00:00
|
|
|
/* Set input filename and number */
|
2004-01-05 11:49:55 +00:00
|
|
|
compressed_name = argv[optind];
|
|
|
|
if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) {
|
2002-03-27 17:31:01 +00:00
|
|
|
/* Open input file */
|
2004-01-05 11:49:55 +00:00
|
|
|
src_fd = bb_xopen(compressed_name, O_RDONLY);
|
|
|
|
} else {
|
2004-03-27 10:02:48 +00:00
|
|
|
src_fd = STDIN_FILENO;
|
2004-01-05 11:49:55 +00:00
|
|
|
opt |= BUNZIP2_OPT_STDOUT;
|
2001-10-05 03:48:57 +00:00
|
|
|
}
|
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) {
|
2003-03-19 09:13:01 +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
|
|
|
|
2004-01-05 11:49:55 +00:00
|
|
|
if (opt & BUNZIP2_OPT_STDOUT) {
|
2004-03-27 10:02:48 +00:00
|
|
|
dst_fd = STDOUT_FILENO;
|
2001-11-18 14:20:25 +00:00
|
|
|
} else {
|
2004-01-05 11:49:55 +00:00
|
|
|
int len = strlen(compressed_name) - 4;
|
|
|
|
if (strcmp(compressed_name + len, ".bz2") != 0) {
|
|
|
|
bb_error_msg_and_die("Invalid extension");
|
|
|
|
}
|
|
|
|
save_name = bb_xstrndup(compressed_name, len);
|
2003-03-19 09:13:01 +00:00
|
|
|
dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
|
2001-11-18 14:20:25 +00:00
|
|
|
}
|
2001-10-05 03:48:57 +00:00
|
|
|
|
2003-10-29 03:37:54 +00:00
|
|
|
status = uncompressStream(src_fd, dst_fd);
|
2004-01-05 11:49:55 +00:00
|
|
|
if(!(opt & BUNZIP2_OPT_STDOUT)) {
|
|
|
|
char *delete_name;
|
2003-10-29 03:37:54 +00:00
|
|
|
if (status) {
|
2002-03-27 17:46:44 +00:00
|
|
|
delete_name = save_name;
|
2003-10-29 03:37:54 +00:00
|
|
|
} else {
|
2004-01-05 11:49:55 +00:00
|
|
|
delete_name = compressed_name;
|
|
|
|
}
|
|
|
|
if (unlink(delete_name) < 0) {
|
|
|
|
bb_error_msg_and_die("Couldn't remove %s", delete_name);
|
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
|
|
|
}
|