"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 | { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 17 | USE_DESKTOP(long long) int status; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 18 | char *filename; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 19 | unsigned opt; |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 20 | int src_fd, dst_fd; |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 22 | opt = getopt32(argc, argv, "cf"); |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 23 | |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 24 | /* Set input filename and number */ |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 25 | filename = argv[optind]; |
| 26 | if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) { |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 27 | /* Open input file */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 28 | src_fd = xopen(filename, O_RDONLY); |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 29 | } else { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 30 | src_fd = STDIN_FILENO; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 31 | filename = 0; |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 32 | } |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 33 | |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 34 | /* if called as bzcat force the stdout flag */ |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 35 | if ((opt & BUNZIP2_OPT_STDOUT) || applet_name[2] == 'c') |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 36 | filename = 0; |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 37 | |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 38 | /* Check that the input is sane. */ |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 39 | if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) { |
Denis Vlasenko | 9c267b8 | 2006-10-12 20:06:18 +0000 | [diff] [blame] | 40 | bb_error_msg_and_die("compressed data not read from terminal, " |
| 41 | "use -f to force it"); |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 42 | } |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 43 | |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 44 | if (filename) { |
Rob Landley | 9a202c9 | 2006-06-13 14:54:42 +0000 | [diff] [blame] | 45 | struct stat stat_buf; |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 46 | /* extension = filename+strlen(filename)-4 is buggy: |
| 47 | * strlen may be less than 4 */ |
| 48 | char *extension = strrchr(filename, '.'); |
| 49 | if (!extension || strcmp(extension, ".bz2") != 0) { |
Denis Vlasenko | ce97960 | 2006-09-27 23:31:08 +0000 | [diff] [blame] | 50 | bb_error_msg_and_die("invalid extension"); |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 51 | } |
Rob Landley | c4b6739 | 2006-06-13 16:09:16 +0000 | [diff] [blame] | 52 | xstat(filename, &stat_buf); |
Denis Vlasenko | 22dca23 | 2006-09-03 14:23:29 +0000 | [diff] [blame] | 53 | *extension = '\0'; |
| 54 | dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC, |
| 55 | stat_buf.st_mode); |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 56 | } else dst_fd = STDOUT_FILENO; |
Glenn L McGrath | f235d05 | 2003-10-29 03:37:54 +0000 | [diff] [blame] | 57 | status = uncompressStream(src_fd, dst_fd); |
Denis Vlasenko | ce97960 | 2006-09-27 23:31:08 +0000 | [diff] [blame] | 58 | if (filename) { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 59 | if (status >= 0) filename[strlen(filename)] = '.'; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 60 | if (unlink(filename) < 0) { |
Denis Vlasenko | ce97960 | 2006-09-27 23:31:08 +0000 | [diff] [blame] | 61 | bb_error_msg_and_die("cannot remove %s", filename); |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 62 | } |
Matt Kraai | cf32ac5 | 2002-03-27 17:46:44 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return status; |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 66 | } |