2002-11-03 12:58:38 +05:30
|
|
|
/*
|
|
|
|
* Modified for busybox by Glenn McGrath <bug1@optushome.com.au>
|
|
|
|
* 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
|
|
|
|
|
|
|
int bunzip2_main(int argc, char **argv)
|
|
|
|
{
|
2001-11-18 19:50:25 +05:30
|
|
|
const int bunzip_to_stdout = 1;
|
2002-03-27 23:01:01 +05:30
|
|
|
const int bunzip_force = 2;
|
2001-11-18 19:50:25 +05:30
|
|
|
int flags = 0;
|
|
|
|
int opt = 0;
|
2002-03-27 23:16:44 +05:30
|
|
|
int status;
|
2001-11-18 19:50:25 +05:30
|
|
|
|
2002-11-03 19:35:15 +05:30
|
|
|
int src_fd;
|
|
|
|
int dst_fd;
|
2002-03-27 23:01:01 +05:30
|
|
|
char *save_name = NULL;
|
2002-03-27 23:16:44 +05:30
|
|
|
char *delete_name = NULL;
|
2001-11-18 19:50:25 +05:30
|
|
|
|
|
|
|
/* if called as bzcat */
|
2003-03-19 14:43:01 +05:30
|
|
|
if (strcmp(bb_applet_name, "bzcat") == 0)
|
2001-11-18 19:50:25 +05:30
|
|
|
flags |= bunzip_to_stdout;
|
|
|
|
|
2002-03-27 23:01:01 +05:30
|
|
|
while ((opt = getopt(argc, argv, "cfh")) != -1) {
|
2001-11-18 19:50:25 +05:30
|
|
|
switch (opt) {
|
|
|
|
case 'c':
|
|
|
|
flags |= bunzip_to_stdout;
|
|
|
|
break;
|
2002-03-27 23:01:01 +05:30
|
|
|
case 'f':
|
|
|
|
flags |= bunzip_force;
|
|
|
|
break;
|
2001-11-18 19:50:25 +05:30
|
|
|
case 'h':
|
|
|
|
default:
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage(); /* exit's inside usage */
|
2001-11-18 19:50:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-27 23:01:01 +05:30
|
|
|
/* Set input filename and number */
|
|
|
|
if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
|
|
|
|
flags |= bunzip_to_stdout;
|
2002-11-03 19:35:15 +05:30
|
|
|
src_fd = fileno(stdin);
|
2002-03-27 23:01:01 +05:30
|
|
|
} else {
|
|
|
|
/* Open input file */
|
2003-03-19 14:43:01 +05:30
|
|
|
src_fd = bb_xopen(argv[optind], O_RDONLY);
|
2001-11-18 19:50:25 +05:30
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
save_name = bb_xstrdup(argv[optind]);
|
2002-03-27 23:01:01 +05:30
|
|
|
if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("Invalid extension");
|
2002-03-27 23:01:01 +05:30
|
|
|
save_name[strlen(save_name) - 4] = '\0';
|
2001-10-05 09:18:57 +05:30
|
|
|
}
|
2002-03-27 23:01:01 +05:30
|
|
|
|
|
|
|
/* Check that the input is sane. */
|
2002-11-03 19:35:15 +05:30
|
|
|
if (isatty(src_fd) && (flags & bunzip_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
|
|
|
|
|
|
|
if (flags & bunzip_to_stdout) {
|
2002-11-03 19:35:15 +05:30
|
|
|
dst_fd = fileno(stdout);
|
2001-11-18 19:50:25 +05:30
|
|
|
} else {
|
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);
|
|
|
|
if(!(flags & bunzip_to_stdout)) {
|
|
|
|
if (status) {
|
2002-03-27 23:16:44 +05:30
|
|
|
delete_name = save_name;
|
2003-10-29 09:07:54 +05:30
|
|
|
} else {
|
|
|
|
delete_name = argv[optind];
|
2002-11-03 19:35:15 +05:30
|
|
|
}
|
2002-03-27 23:16:44 +05:30
|
|
|
}
|
|
|
|
|
2002-11-03 19:35:15 +05:30
|
|
|
if ((delete_name) && (unlink(delete_name) < 0)) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("Couldn't remove %s", delete_name);
|
2002-03-27 23:16:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2001-10-05 09:18:57 +05:30
|
|
|
}
|