1 /* vi: set sw=4 ts=4: */
3 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
4 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16 #include "unarchive.h"
18 #define BUNZIP2_OPT_STDOUT 1
19 #define BUNZIP2_OPT_FORCE 2
21 int bunzip2_main(int argc, char **argv)
25 int status, src_fd, dst_fd;
27 opt = bb_getopt_ulflags(argc, argv, "cf");
29 /* Set input filename and number */
30 filename = argv[optind];
31 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
33 src_fd = bb_xopen(filename, O_RDONLY);
35 src_fd = STDIN_FILENO;
39 /* if called as bzcat force the stdout flag */
40 if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c')
43 /* Check that the input is sane. */
44 if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
45 bb_error_msg_and_die("Compressed data not read from terminal. Use -f to force it.");
50 char *extension=filename+strlen(filename)-4;
51 if (strcmp(extension, ".bz2") != 0) {
52 bb_error_msg_and_die("Invalid extension");
54 xstat(filename, &stat_buf);
56 dst_fd = bb_xopen3(filename, O_WRONLY | O_CREAT, stat_buf.st_mode);
57 } else dst_fd = STDOUT_FILENO;
58 status = uncompressStream(src_fd, dst_fd);
60 if (!status) filename[strlen(filename)]='.';
61 if (unlink(filename) < 0) {
62 bb_error_msg_and_die("Couldn't remove %s", filename);