blob: 5cd013c0ce87d72948caa915f2b1d60eedbb4d5a [file] [log] [blame]
Glenn L McGrath60bce492002-11-03 07:28:38 +00001/*
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00002 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrath60bce492002-11-03 07:28:38 +00003 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
4 *
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.
9 *
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.
14 *
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.
18 */
Glenn L McGrath24e28332001-10-05 03:48:57 +000019
Glenn L McGrath237ae422002-11-03 14:05:15 +000020#include <fcntl.h>
Glenn L McGrathfff11f12001-11-18 14:20:25 +000021#include <getopt.h>
Glenn L McGrath60bce492002-11-03 07:28:38 +000022#include <stdio.h>
23#include <stdlib.h>
Glenn L McGrath237ae422002-11-03 14:05:15 +000024#include <string.h>
Matt Kraai9cdb0602002-03-27 17:31:01 +000025#include <unistd.h>
Glenn L McGrath24e28332001-10-05 03:48:57 +000026
Glenn L McGrath60bce492002-11-03 07:28:38 +000027#include "busybox.h"
28#include "unarchive.h"
Glenn L McGrath24e28332001-10-05 03:48:57 +000029
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000030#define BUNZIP2_OPT_STDOUT 1
31#define BUNZIP2_OPT_FORCE 2
32
Glenn L McGrath24e28332001-10-05 03:48:57 +000033int bunzip2_main(int argc, char **argv)
34{
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000035 char *compressed_name;
Eric Andersenb225e2a2004-08-28 00:43:07 +000036 /* Note: Ignore the warning about save_name being used uninitialized.
37 * That is not the case, but gcc has trouble working that out... */
Mike Frysingerd6a2d412005-07-30 08:57:35 +000038#warning The save_name warning is OK, ignore it
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000039 char *save_name;
40 unsigned long opt;
Matt Kraaicf32ac52002-03-27 17:46:44 +000041 int status;
Glenn L McGrath237ae422002-11-03 14:05:15 +000042 int src_fd;
43 int dst_fd;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000044
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000045 opt = bb_getopt_ulflags(argc, argv, "cf");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000046
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000047 /* if called as bzcat force the stdout flag */
48 if (bb_applet_name[2] == 'c') {
49 opt |= BUNZIP2_OPT_STDOUT;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000050 }
51
Matt Kraai9cdb0602002-03-27 17:31:01 +000052 /* Set input filename and number */
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000053 compressed_name = argv[optind];
54 if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) {
Matt Kraai9cdb0602002-03-27 17:31:01 +000055 /* Open input file */
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000056 src_fd = bb_xopen(compressed_name, O_RDONLY);
57 } else {
Eric Andersen70060d22004-03-27 10:02:48 +000058 src_fd = STDIN_FILENO;
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000059 opt |= BUNZIP2_OPT_STDOUT;
Glenn L McGrath24e28332001-10-05 03:48:57 +000060 }
Glenn L McGrathfff11f12001-11-18 14:20:25 +000061
Matt Kraai9cdb0602002-03-27 17:31:01 +000062 /* Check that the input is sane. */
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000063 if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000065 }
Glenn L McGrath24e28332001-10-05 03:48:57 +000066
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000067 if (opt & BUNZIP2_OPT_STDOUT) {
Eric Andersen70060d22004-03-27 10:02:48 +000068 dst_fd = STDOUT_FILENO;
Glenn L McGrath237ae422002-11-03 14:05:15 +000069 } else {
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000070 int len = strlen(compressed_name) - 4;
71 if (strcmp(compressed_name + len, ".bz2") != 0) {
72 bb_error_msg_and_die("Invalid extension");
73 }
74 save_name = bb_xstrndup(compressed_name, len);
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
Glenn L McGrath237ae422002-11-03 14:05:15 +000076 }
77
Glenn L McGrathf235d052003-10-29 03:37:54 +000078 status = uncompressStream(src_fd, dst_fd);
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000079 if(!(opt & BUNZIP2_OPT_STDOUT)) {
80 char *delete_name;
Glenn L McGrathf235d052003-10-29 03:37:54 +000081 if (status) {
82 delete_name = save_name;
83 } else {
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000084 delete_name = compressed_name;
Glenn L McGrath237ae422002-11-03 14:05:15 +000085 }
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000086 if (unlink(delete_name) < 0) {
87 bb_error_msg_and_die("Couldn't remove %s", delete_name);
88 }
Matt Kraaicf32ac52002-03-27 17:46:44 +000089 }
90
91 return status;
Glenn L McGrath24e28332001-10-05 03:48:57 +000092}