"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 2 | /* |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 3 | * Modified for busybox by Glenn McGrath <bug1@iinet.net.au> |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 4 | * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no> |
| 5 | * |
Rob Landley | 0edbad1 | 2006-04-17 22:49:30 +0000 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 7 | */ |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 8 | |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 9 | #include "busybox.h" |
| 10 | #include "unarchive.h" |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 11 | |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 12 | #define BUNZIP2_OPT_STDOUT 1 |
| 13 | #define BUNZIP2_OPT_FORCE 2 |
| 14 | |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 15 | int bunzip2_main(int argc, char **argv) |
| 16 | { |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 17 | char *filename; |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 18 | unsigned long opt; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 19 | int status, src_fd, dst_fd; |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 20 | |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 21 | opt = bb_getopt_ulflags(argc, argv, "cf"); |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 22 | |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 23 | /* Set input filename and number */ |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 24 | filename = argv[optind]; |
| 25 | if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) { |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 26 | /* Open input file */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 27 | src_fd = xopen(filename, O_RDONLY); |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 28 | } else { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 29 | src_fd = STDIN_FILENO; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 30 | filename = 0; |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 31 | } |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 32 | |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 33 | /* if called as bzcat force the stdout flag */ |
| 34 | if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c') |
| 35 | filename = 0; |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 36 | |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 37 | /* Check that the input is sane. */ |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 38 | if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) { |
Rob Landley | 9a202c9 | 2006-06-13 14:54:42 +0000 | [diff] [blame] | 39 | 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] | 40 | } |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 41 | |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 42 | if (filename) { |
Rob Landley | 9a202c9 | 2006-06-13 14:54:42 +0000 | [diff] [blame] | 43 | struct stat stat_buf; |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 44 | /* extension = filename+strlen(filename)-4 is buggy: |
| 45 | * strlen may be less than 4 */ |
| 46 | char *extension = strrchr(filename, '.'); |
| 47 | if (!extension || strcmp(extension, ".bz2") != 0) { |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 48 | bb_error_msg_and_die("Invalid extension"); |
| 49 | } |
Rob Landley | c4b6739 | 2006-06-13 16:09:16 +0000 | [diff] [blame] | 50 | xstat(filename, &stat_buf); |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 51 | *extension = '\0'; |
| 52 | dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC, |
| 53 | stat_buf.st_mode); |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 54 | } else dst_fd = STDOUT_FILENO; |
Glenn L McGrath | f235d05 | 2003-10-29 03:37:54 +0000 | [diff] [blame] | 55 | status = uncompressStream(src_fd, dst_fd); |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 56 | if(filename) { |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 57 | if (!status) filename[strlen(filename)] = '.'; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 58 | if (unlink(filename) < 0) { |
| 59 | bb_error_msg_and_die("Couldn't remove %s", filename); |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 60 | } |
Matt Kraai | cf32ac5 | 2002-03-27 17:46:44 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return status; |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 64 | } |