2 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
3 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "unarchive.h"
30 #define BUNZIP2_OPT_STDOUT 1
31 #define BUNZIP2_OPT_FORCE 2
33 int bunzip2_main(int argc, char **argv)
35 char *compressed_name;
36 /* Note: Ignore the warning about save_name being used uninitialized.
37 * That is not the case, but gcc has trouble working that out... */
38 #warning The save_name warning is OK, ignore it
45 opt = bb_getopt_ulflags(argc, argv, "cf");
47 /* if called as bzcat force the stdout flag */
48 if (bb_applet_name[2] == 'c') {
49 opt |= BUNZIP2_OPT_STDOUT;
52 /* Set input filename and number */
53 compressed_name = argv[optind];
54 if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) {
56 src_fd = bb_xopen(compressed_name, O_RDONLY);
58 src_fd = STDIN_FILENO;
59 opt |= BUNZIP2_OPT_STDOUT;
62 /* Check that the input is sane. */
63 if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
64 bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
67 if (opt & BUNZIP2_OPT_STDOUT) {
68 dst_fd = STDOUT_FILENO;
70 int len = strlen(compressed_name) - 4;
71 if (strcmp(compressed_name + len, ".bz2") != 0) {
72 bb_error_msg_and_die("Invalid extension");
74 save_name = bb_xstrndup(compressed_name, len);
75 dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
78 status = uncompressStream(src_fd, dst_fd);
79 if(!(opt & BUNZIP2_OPT_STDOUT)) {
82 delete_name = save_name;
84 delete_name = compressed_name;
86 if (unlink(delete_name) < 0) {
87 bb_error_msg_and_die("Couldn't remove %s", delete_name);