blob: 757001fe463586ec5b8411d9761836b897c5bc98 [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 McGrath60bce492002-11-03 07:28:38 +00009#include "busybox.h"
10#include "unarchive.h"
Glenn L McGrath24e28332001-10-05 03:48:57 +000011
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000012#define BUNZIP2_OPT_STDOUT 1
13#define BUNZIP2_OPT_FORCE 2
14
Glenn L McGrath24e28332001-10-05 03:48:57 +000015int bunzip2_main(int argc, char **argv)
16{
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000017 USE_DESKTOP(long long) int status;
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000018 char *filename;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000019 unsigned opt;
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000020 int src_fd, dst_fd;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000021
Denis Vlasenko67b23e62006-10-03 21:00:06 +000022 opt = getopt32(argc, argv, "cf");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000023
Matt Kraai9cdb0602002-03-27 17:31:01 +000024 /* Set input filename and number */
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000025 filename = argv[optind];
26 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
Matt Kraai9cdb0602002-03-27 17:31:01 +000027 /* Open input file */
Rob Landleyd921b2e2006-08-03 15:41:12 +000028 src_fd = xopen(filename, O_RDONLY);
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000029 } else {
Eric Andersen70060d22004-03-27 10:02:48 +000030 src_fd = STDIN_FILENO;
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000031 filename = 0;
Glenn L McGrath24e28332001-10-05 03:48:57 +000032 }
Rob Landleyb7128c62005-09-11 01:05:30 +000033
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000034 /* if called as bzcat force the stdout flag */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000035 if ((opt & BUNZIP2_OPT_STDOUT) || applet_name[2] == 'c')
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000036 filename = 0;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000037
Matt Kraai9cdb0602002-03-27 17:31:01 +000038 /* Check that the input is sane. */
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000039 if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
Denis Vlasenko9c267b82006-10-12 20:06:18 +000040 bb_error_msg_and_die("compressed data not read from terminal, "
41 "use -f to force it");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000042 }
Glenn L McGrath24e28332001-10-05 03:48:57 +000043
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000044 if (filename) {
Rob Landley9a202c92006-06-13 14:54:42 +000045 struct stat stat_buf;
Denis Vlasenko22dca232006-09-03 14:23:29 +000046 /* 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 Vlasenkoce979602006-09-27 23:31:08 +000050 bb_error_msg_and_die("invalid extension");
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000051 }
Rob Landleyc4b67392006-06-13 16:09:16 +000052 xstat(filename, &stat_buf);
Denis Vlasenko22dca232006-09-03 14:23:29 +000053 *extension = '\0';
54 dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
55 stat_buf.st_mode);
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000056 } else dst_fd = STDOUT_FILENO;
Glenn L McGrathf235d052003-10-29 03:37:54 +000057 status = uncompressStream(src_fd, dst_fd);
Denis Vlasenkoce979602006-09-27 23:31:08 +000058 if (filename) {
Denis Vlasenko97a8dd32006-10-01 15:55:11 +000059 if (status >= 0) filename[strlen(filename)] = '.';
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000060 if (unlink(filename) < 0) {
Denis Vlasenkoce979602006-09-27 23:31:08 +000061 bb_error_msg_and_die("cannot remove %s", filename);
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000062 }
Matt Kraaicf32ac52002-03-27 17:46:44 +000063 }
64
65 return status;
Glenn L McGrath24e28332001-10-05 03:48:57 +000066}