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