Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 1 | /* |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 2 | * Modified for busybox by Glenn McGrath <bug1@iinet.net.au> |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 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 McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 19 | |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 21 | #include <getopt.h> |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 24 | #include <string.h> |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 26 | |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
| 28 | #include "unarchive.h" |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 29 | |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 30 | #define BUNZIP2_OPT_STDOUT 1 |
| 31 | #define BUNZIP2_OPT_FORCE 2 |
| 32 | |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 33 | int bunzip2_main(int argc, char **argv) |
| 34 | { |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 35 | char *compressed_name; |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 36 | /* Note: Ignore the warning about save_name being used uninitialized. |
| 37 | * That is not the case, but gcc has trouble working that out... */ |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 38 | char *save_name; |
| 39 | unsigned long opt; |
Matt Kraai | cf32ac5 | 2002-03-27 17:46:44 +0000 | [diff] [blame] | 40 | int status; |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 41 | int src_fd; |
| 42 | int dst_fd; |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 43 | |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 44 | opt = bb_getopt_ulflags(argc, argv, "cf"); |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 45 | |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 46 | /* if called as bzcat force the stdout flag */ |
| 47 | if (bb_applet_name[2] == 'c') { |
| 48 | opt |= BUNZIP2_OPT_STDOUT; |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 51 | /* Set input filename and number */ |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 52 | compressed_name = argv[optind]; |
| 53 | if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) { |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 54 | /* Open input file */ |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 55 | src_fd = bb_xopen(compressed_name, O_RDONLY); |
| 56 | } else { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 57 | src_fd = STDIN_FILENO; |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 58 | opt |= BUNZIP2_OPT_STDOUT; |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 59 | } |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 60 | |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 61 | /* Check that the input is sane. */ |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 62 | if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it."); |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 64 | } |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 65 | |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 66 | if (opt & BUNZIP2_OPT_STDOUT) { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 67 | dst_fd = STDOUT_FILENO; |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 68 | } else { |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 69 | int len = strlen(compressed_name) - 4; |
| 70 | if (strcmp(compressed_name + len, ".bz2") != 0) { |
| 71 | bb_error_msg_and_die("Invalid extension"); |
| 72 | } |
| 73 | save_name = bb_xstrndup(compressed_name, len); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT); |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Glenn L McGrath | f235d05 | 2003-10-29 03:37:54 +0000 | [diff] [blame] | 77 | status = uncompressStream(src_fd, dst_fd); |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 78 | if(!(opt & BUNZIP2_OPT_STDOUT)) { |
| 79 | char *delete_name; |
Glenn L McGrath | f235d05 | 2003-10-29 03:37:54 +0000 | [diff] [blame] | 80 | if (status) { |
| 81 | delete_name = save_name; |
| 82 | } else { |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 83 | delete_name = compressed_name; |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 84 | } |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 85 | if (unlink(delete_name) < 0) { |
| 86 | bb_error_msg_and_die("Couldn't remove %s", delete_name); |
| 87 | } |
Matt Kraai | cf32ac5 | 2002-03-27 17:46:44 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return status; |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 91 | } |