blob: 714dac07744bf892096f9cd9caa9ddc28b0fc3cb [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath60bce492002-11-03 07:28:38 +00002/*
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00003 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrath60bce492002-11-03 07:28:38 +00004 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
5 *
Rob Landley0edbad12006-04-17 22:49:30 +00006 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath60bce492002-11-03 07:28:38 +00007 */
Glenn L McGrath24e28332001-10-05 03:48:57 +00008
Glenn L McGrath237ae422002-11-03 14:05:15 +00009#include <fcntl.h>
Glenn L McGrath60bce492002-11-03 07:28:38 +000010#include <stdio.h>
11#include <stdlib.h>
Glenn L McGrath237ae422002-11-03 14:05:15 +000012#include <string.h>
Matt Kraai9cdb0602002-03-27 17:31:01 +000013#include <unistd.h>
Glenn L McGrath24e28332001-10-05 03:48:57 +000014
Glenn L McGrath60bce492002-11-03 07:28:38 +000015#include "busybox.h"
16#include "unarchive.h"
Glenn L McGrath24e28332001-10-05 03:48:57 +000017
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000018#define BUNZIP2_OPT_STDOUT 1
19#define BUNZIP2_OPT_FORCE 2
20
Glenn L McGrath24e28332001-10-05 03:48:57 +000021int bunzip2_main(int argc, char **argv)
22{
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000023 char *filename;
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000024 unsigned long opt;
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000025 int status, src_fd, dst_fd;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000026
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000027 opt = bb_getopt_ulflags(argc, argv, "cf");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000028
Matt Kraai9cdb0602002-03-27 17:31:01 +000029 /* Set input filename and number */
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000030 filename = argv[optind];
31 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
Matt Kraai9cdb0602002-03-27 17:31:01 +000032 /* Open input file */
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000033 src_fd = bb_xopen(filename, O_RDONLY);
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000034 } else {
Eric Andersen70060d22004-03-27 10:02:48 +000035 src_fd = STDIN_FILENO;
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000036 filename = 0;
Glenn L McGrath24e28332001-10-05 03:48:57 +000037 }
Rob Landleyb7128c62005-09-11 01:05:30 +000038
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000039 /* if called as bzcat force the stdout flag */
40 if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c')
41 filename = 0;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000042
Matt Kraai9cdb0602002-03-27 17:31:01 +000043 /* Check that the input is sane. */
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000044 if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
Rob Landley9a202c92006-06-13 14:54:42 +000045 bb_error_msg_and_die("Compressed data not read from terminal. Use -f to force it.");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000046 }
Glenn L McGrath24e28332001-10-05 03:48:57 +000047
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000048 if (filename) {
Rob Landley9a202c92006-06-13 14:54:42 +000049 struct stat stat_buf;
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000050 char *extension=filename+strlen(filename)-4;
51 if (strcmp(extension, ".bz2") != 0) {
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000052 bb_error_msg_and_die("Invalid extension");
53 }
Rob Landleyc4b67392006-06-13 16:09:16 +000054 xstat(filename, &stat_buf);
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000055 *extension=0;
Rob Landley9a202c92006-06-13 14:54:42 +000056 dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode);
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000057 } else dst_fd = STDOUT_FILENO;
Glenn L McGrathf235d052003-10-29 03:37:54 +000058 status = uncompressStream(src_fd, dst_fd);
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000059 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 McGrath6cb3bc02004-01-05 11:49:55 +000063 }
Matt Kraaicf32ac52002-03-27 17:46:44 +000064 }
65
66 return status;
Glenn L McGrath24e28332001-10-05 03:48:57 +000067}