blob: 1b074c4eeae21e7f549c37101d1083574044d26e [file] [log] [blame]
Glenn L McGrath60bce492002-11-03 07:28:38 +00001/*
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00002 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrath60bce492002-11-03 07:28:38 +00003 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
4 *
Rob Landley0edbad12006-04-17 22:49:30 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath60bce492002-11-03 07:28:38 +00006 */
Glenn L McGrath24e28332001-10-05 03:48:57 +00007
Glenn L McGrath237ae422002-11-03 14:05:15 +00008#include <fcntl.h>
Glenn L McGrath60bce492002-11-03 07:28:38 +00009#include <stdio.h>
10#include <stdlib.h>
Glenn L McGrath237ae422002-11-03 14:05:15 +000011#include <string.h>
Matt Kraai9cdb0602002-03-27 17:31:01 +000012#include <unistd.h>
Glenn L McGrath24e28332001-10-05 03:48:57 +000013
Glenn L McGrath60bce492002-11-03 07:28:38 +000014#include "busybox.h"
15#include "unarchive.h"
Glenn L McGrath24e28332001-10-05 03:48:57 +000016
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000017#define BUNZIP2_OPT_STDOUT 1
18#define BUNZIP2_OPT_FORCE 2
19
Glenn L McGrath24e28332001-10-05 03:48:57 +000020int bunzip2_main(int argc, char **argv)
21{
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000022 char *filename;
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000023 unsigned long opt;
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000024 int status, src_fd, dst_fd;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000025
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000026 opt = bb_getopt_ulflags(argc, argv, "cf");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000027
Matt Kraai9cdb0602002-03-27 17:31:01 +000028 /* Set input filename and number */
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000029 filename = argv[optind];
30 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
Matt Kraai9cdb0602002-03-27 17:31:01 +000031 /* Open input file */
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000032 src_fd = bb_xopen(filename, O_RDONLY);
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000033 } else {
Eric Andersen70060d22004-03-27 10:02:48 +000034 src_fd = STDIN_FILENO;
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000035 filename = 0;
Glenn L McGrath24e28332001-10-05 03:48:57 +000036 }
Rob Landleyb7128c62005-09-11 01:05:30 +000037
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000038 /* if called as bzcat force the stdout flag */
39 if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c')
40 filename = 0;
Glenn L McGrathfff11f12001-11-18 14:20:25 +000041
Matt Kraai9cdb0602002-03-27 17:31:01 +000042 /* Check that the input is sane. */
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000043 if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
Glenn L McGrathfff11f12001-11-18 14:20:25 +000045 }
Glenn L McGrath24e28332001-10-05 03:48:57 +000046
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000047 if (filename) {
48 char *extension=filename+strlen(filename)-4;
49 if (strcmp(extension, ".bz2") != 0) {
Glenn L McGrath6cb3bc02004-01-05 11:49:55 +000050 bb_error_msg_and_die("Invalid extension");
51 }
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000052 *extension=0;
53 dst_fd = bb_xopen(filename, O_WRONLY | O_CREAT);
54 } else dst_fd = STDOUT_FILENO;
Glenn L McGrathf235d052003-10-29 03:37:54 +000055 status = uncompressStream(src_fd, dst_fd);
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000056 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 McGrath6cb3bc02004-01-05 11:49:55 +000060 }
Matt Kraaicf32ac52002-03-27 17:46:44 +000061 }
62
63 return status;
Glenn L McGrath24e28332001-10-05 03:48:57 +000064}
Rob Landleyc8b8a2d2005-08-30 20:26:17 +000065/* vi:set ts=4: */