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) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +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) { |
| 48 | char *extension=filename+strlen(filename)-4; |
| 49 | if (strcmp(extension, ".bz2") != 0) { |
Glenn L McGrath | 6cb3bc0 | 2004-01-05 11:49:55 +0000 | [diff] [blame] | 50 | bb_error_msg_and_die("Invalid extension"); |
| 51 | } |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 52 | *extension=0; |
| 53 | dst_fd = bb_xopen(filename, O_WRONLY | O_CREAT); |
| 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) { |
| 57 | if (!status) filename[strlen(filename)]='.'; |
| 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 | } |
Rob Landley | c8b8a2d | 2005-08-30 20:26:17 +0000 | [diff] [blame] | 65 | /* vi:set ts=4: */ |