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>
|
|
|
|
*
|
|
|
|
* 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 09:18:57 +05:30
|
|
|
|
2002-11-03 19:35:15 +05:30
|
|
|
#include <fcntl.h>
|
2001-11-18 19:50:25 +05:30
|
|
|
#include <getopt.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)
|
|
|
|
{
|
2004-01-05 17:19:55 +05:30
|
|
|
char *compressed_name;
|
2004-08-28 06:13:07 +05:30
|
|
|
/* Note: Ignore the warning about save_name being used uninitialized.
|
|
|
|
* That is not the case, but gcc has trouble working that out... */
|
2005-07-30 14:27:35 +05:30
|
|
|
#warning The save_name warning is OK, ignore it
|
2004-01-05 17:19:55 +05:30
|
|
|
char *save_name;
|
|
|
|
unsigned long opt;
|
2002-03-27 23:16:44 +05:30
|
|
|
int status;
|
2002-11-03 19:35:15 +05:30
|
|
|
int src_fd;
|
|
|
|
int 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
|
|
|
|
2004-01-05 17:19:55 +05:30
|
|
|
/* if called as bzcat force the stdout flag */
|
|
|
|
if (bb_applet_name[2] == 'c') {
|
|
|
|
opt |= BUNZIP2_OPT_STDOUT;
|
2001-11-18 19:50:25 +05:30
|
|
|
}
|
|
|
|
|
2002-03-27 23:01:01 +05:30
|
|
|
/* Set input filename and number */
|
2004-01-05 17:19:55 +05:30
|
|
|
compressed_name = argv[optind];
|
|
|
|
if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) {
|
2002-03-27 23:01:01 +05:30
|
|
|
/* Open input file */
|
2004-01-05 17:19:55 +05:30
|
|
|
src_fd = bb_xopen(compressed_name, O_RDONLY);
|
|
|
|
} else {
|
2004-03-27 15:32:48 +05:30
|
|
|
src_fd = STDIN_FILENO;
|
2004-01-05 17:19:55 +05:30
|
|
|
opt |= BUNZIP2_OPT_STDOUT;
|
2001-10-05 09:18:57 +05:30
|
|
|
}
|
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) {
|
2003-03-19 14:43:01 +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
|
|
|
|
2004-01-05 17:19:55 +05:30
|
|
|
if (opt & BUNZIP2_OPT_STDOUT) {
|
2004-03-27 15:32:48 +05:30
|
|
|
dst_fd = STDOUT_FILENO;
|
2001-11-18 19:50:25 +05:30
|
|
|
} else {
|
2004-01-05 17:19:55 +05:30
|
|
|
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 14:43:01 +05:30
|
|
|
dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
|
2001-11-18 19:50:25 +05:30
|
|
|
}
|
2001-10-05 09:18:57 +05:30
|
|
|
|
2003-10-29 09:07:54 +05:30
|
|
|
status = uncompressStream(src_fd, dst_fd);
|
2004-01-05 17:19:55 +05:30
|
|
|
if(!(opt & BUNZIP2_OPT_STDOUT)) {
|
|
|
|
char *delete_name;
|
2003-10-29 09:07:54 +05:30
|
|
|
if (status) {
|
2002-03-27 23:16:44 +05:30
|
|
|
delete_name = save_name;
|
2003-10-29 09:07:54 +05:30
|
|
|
} else {
|
2004-01-05 17:19:55 +05:30
|
|
|
delete_name = compressed_name;
|
|
|
|
}
|
|
|
|
if (unlink(delete_name) < 0) {
|
|
|
|
bb_error_msg_and_die("Couldn't remove %s", delete_name);
|
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
|
|
|
}
|