2 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
3 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
5 * Licensed under GPL v2, see file LICENSE in this tarball for details.
15 #include "unarchive.h"
17 #define BUNZIP2_OPT_STDOUT 1
18 #define BUNZIP2_OPT_FORCE 2
20 int bunzip2_main(int argc, char **argv)
24 int status, src_fd, dst_fd;
26 opt = bb_getopt_ulflags(argc, argv, "cf");
28 /* Set input filename and number */
29 filename = argv[optind];
30 if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
32 src_fd = bb_xopen(filename, O_RDONLY);
34 src_fd = STDIN_FILENO;
38 /* if called as bzcat force the stdout flag */
39 if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c')
42 /* Check that the input is sane. */
43 if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
44 bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
48 char *extension=filename+strlen(filename)-4;
49 if (strcmp(extension, ".bz2") != 0) {
50 bb_error_msg_and_die("Invalid extension");
53 dst_fd = bb_xopen(filename, O_WRONLY | O_CREAT);
54 } else dst_fd = STDOUT_FILENO;
55 status = uncompressStream(src_fd, dst_fd);
57 if (!status) filename[strlen(filename)]='.';
58 if (unlink(filename) < 0) {
59 bb_error_msg_and_die("Couldn't remove %s", filename);