blob: 4f231c959f9c22f8701902a9aed187b35190644e [file] [log] [blame]
Glenn L McGrath60bce492002-11-03 07:28:38 +00001/*
2 * Modified for busybox by Glenn McGrath <bug1@optushome.com.au>
3 * 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
30int bunzip2_main(int argc, char **argv)
31{
Glenn L McGrathfff11f12001-11-18 14:20:25 +000032 const int bunzip_to_stdout = 1;
Matt Kraai9cdb0602002-03-27 17:31:01 +000033 const int bunzip_force = 2;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000034 int flags = 0;
35 int opt = 0;
Matt Kraaicf32ac52002-03-27 17:46:44 +000036 int status;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000037
Glenn L McGrath237ae422002-11-03 14:05:15 +000038 int src_fd;
39 int dst_fd;
Matt Kraai9cdb0602002-03-27 17:31:01 +000040 char *save_name = NULL;
Matt Kraaicf32ac52002-03-27 17:46:44 +000041 char *delete_name = NULL;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000042
43 /* if called as bzcat */
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 if (strcmp(bb_applet_name, "bzcat") == 0)
Glenn L McGrathfff11f12001-11-18 14:20:25 +000045 flags |= bunzip_to_stdout;
46
Matt Kraai9cdb0602002-03-27 17:31:01 +000047 while ((opt = getopt(argc, argv, "cfh")) != -1) {
Glenn L McGrathfff11f12001-11-18 14:20:25 +000048 switch (opt) {
49 case 'c':
50 flags |= bunzip_to_stdout;
51 break;
Matt Kraai9cdb0602002-03-27 17:31:01 +000052 case 'f':
53 flags |= bunzip_force;
54 break;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000055 case 'h':
56 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_show_usage(); /* exit's inside usage */
Glenn L McGrathfff11f12001-11-18 14:20:25 +000058 }
59 }
60
Matt Kraai9cdb0602002-03-27 17:31:01 +000061 /* Set input filename and number */
62 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
63 flags |= bunzip_to_stdout;
Glenn L McGrath237ae422002-11-03 14:05:15 +000064 src_fd = fileno(stdin);
Matt Kraai9cdb0602002-03-27 17:31:01 +000065 } else {
66 /* Open input file */
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 src_fd = bb_xopen(argv[optind], O_RDONLY);
Matt Kraai9cdb0602002-03-27 17:31:01 +000068
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 save_name = bb_xstrdup(argv[optind]);
Matt Kraai9cdb0602002-03-27 17:31:01 +000070 if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 bb_error_msg_and_die("Invalid extension");
Matt Kraai9cdb0602002-03-27 17:31:01 +000072 save_name[strlen(save_name) - 4] = '\0';
Glenn L McGrath24e28332001-10-05 03:48:57 +000073 }
Glenn L McGrathfff11f12001-11-18 14:20:25 +000074
Matt Kraai9cdb0602002-03-27 17:31:01 +000075 /* Check that the input is sane. */
Glenn L McGrath237ae422002-11-03 14:05:15 +000076 if (isatty(src_fd) && (flags & bunzip_force) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000078 }
Glenn L McGrath24e28332001-10-05 03:48:57 +000079
Glenn L McGrath237ae422002-11-03 14:05:15 +000080 if (flags & bunzip_to_stdout) {
81 dst_fd = fileno(stdout);
82 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
Glenn L McGrath237ae422002-11-03 14:05:15 +000084 }
85
Glenn L McGrathdebb21e2003-10-28 23:04:50 +000086 if (uncompressStream(src_fd, dst_fd) == 0) {
Glenn L McGrath237ae422002-11-03 14:05:15 +000087 if (!(flags & bunzip_to_stdout)) {
Matt Kraaicf32ac52002-03-27 17:46:44 +000088 delete_name = argv[optind];
Glenn L McGrath237ae422002-11-03 14:05:15 +000089 }
Matt Kraaicf32ac52002-03-27 17:46:44 +000090 status = EXIT_SUCCESS;
91 } else {
Glenn L McGrath237ae422002-11-03 14:05:15 +000092 if (!(flags & bunzip_to_stdout)) {
Matt Kraaicf32ac52002-03-27 17:46:44 +000093 delete_name = save_name;
Glenn L McGrath237ae422002-11-03 14:05:15 +000094 }
Matt Kraaicf32ac52002-03-27 17:46:44 +000095 status = EXIT_FAILURE;
96 }
97
Glenn L McGrath237ae422002-11-03 14:05:15 +000098 if ((delete_name) && (unlink(delete_name) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 bb_error_msg_and_die("Couldn't remove %s", delete_name);
Matt Kraaicf32ac52002-03-27 17:46:44 +0000100 }
101
102 return status;
Glenn L McGrath24e28332001-10-05 03:48:57 +0000103}