"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 | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 9 | #include <fcntl.h> |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 12 | #include <string.h> |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 13 | #include <unistd.h> |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 14 | |
Glenn L McGrath | 60bce49 | 2002-11-03 07:28:38 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
| 16 | #include "unarchive.h" |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 17 | |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 18 | #define BUNZIP2_OPT_STDOUT 1 |
| 19 | #define BUNZIP2_OPT_FORCE 2 |
| 20 | |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 21 | int bunzip2_main(int argc, char **argv) |
| 22 | { |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 23 | char *filename; |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 24 | unsigned long opt; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 25 | int status, src_fd, dst_fd; |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 26 | |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 27 | opt = bb_getopt_ulflags(argc, argv, "cf"); |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 28 | |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 29 | /* Set input filename and number */ |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 30 | filename = argv[optind]; |
| 31 | if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) { |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 32 | /* Open input file */ |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 33 | src_fd = bb_xopen(filename, O_RDONLY); |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 34 | } else { |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 35 | src_fd = STDIN_FILENO; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 36 | filename = 0; |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 37 | } |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 38 | |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 39 | /* if called as bzcat force the stdout flag */ |
| 40 | if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c') |
| 41 | filename = 0; |
Glenn L McGrath | fff11f1 | 2001-11-18 14:20:25 +0000 | [diff] [blame] | 42 | |
Matt Kraai | 9cdb060 | 2002-03-27 17:31:01 +0000 | [diff] [blame] | 43 | /* Check that the input is sane. */ |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 44 | if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) { |
Rob Landley | 9a202c9 | 2006-06-13 14:54:42 +0000 | [diff] [blame] | 45 | 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] | 46 | } |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 47 | |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 48 | if (filename) { |
Rob Landley | 9a202c9 | 2006-06-13 14:54:42 +0000 | [diff] [blame] | 49 | struct stat stat_buf; |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 50 | char *extension=filename+strlen(filename)-4; |
| 51 | if (strcmp(extension, ".bz2") != 0) { |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 52 | bb_error_msg_and_die("Invalid extension"); |
| 53 | } |
Rob Landley | c4b6739 | 2006-06-13 16:09:16 +0000 | [diff] [blame] | 54 | xstat(filename, &stat_buf); |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 55 | *extension=0; |
Rob Landley | 9a202c9 | 2006-06-13 14:54:42 +0000 | [diff] [blame] | 56 | dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode); |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 57 | } else dst_fd = STDOUT_FILENO; |
Glenn L McGrath | f235d05 | 2003-10-29 03:37:54 +0000 | [diff] [blame] | 58 | status = uncompressStream(src_fd, dst_fd); |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 59 | if(filename) { |
| 60 | if (!status) filename[strlen(filename)]='.'; |
| 61 | if (unlink(filename) < 0) { |
| 62 | bb_error_msg_and_die("Couldn't remove %s", filename); |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 63 | } |
Matt Kraai | cf32ac5 | 2002-03-27 17:46:44 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | return status; |
Glenn L McGrath | 24e2833 | 2001-10-05 03:48:57 +0000 | [diff] [blame] | 67 | } |