From fff11f1ab7ec7cc67087231361f3bde8ecff115f Mon Sep 17 00:00:00 2001 From: Glenn L McGrath Date: Sun, 18 Nov 2001 14:20:25 +0000 Subject: bzcat and bunzip -c support from Thomas Lundquist --- archival/bunzip2.c | 38 ++++++++++++++++++++++++++++++++++---- include/applets.h | 3 +++ include/usage.h | 8 +++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/archival/bunzip2.c b/archival/bunzip2.c index da9808e..290681d 100644 --- a/archival/bunzip2.c +++ b/archival/bunzip2.c @@ -1,4 +1,5 @@ /* Modified for busybox by Glenn McGrath */ +/* Added support output to stdout by Thomas Lundquist */ /*-- This file is a part of bzip2 and/or libbzip2, a program and library for lossless, block-sorting data compression. @@ -56,6 +57,7 @@ #include #include #include +#include #include //#define TRUE 1 @@ -2316,15 +2318,38 @@ errhandler_io: int bunzip2_main(int argc, char **argv) { + const int bunzip_to_stdout = 1; + int flags = 0; + int opt = 0; + FILE *src_stream; FILE *dst_stream; char *save_name; char *save_name_ptr; - if (argc != 2) { + + /* if called as bzcat */ + if (strcmp(applet_name, "bzcat") == 0) + flags |= bunzip_to_stdout; + + while ((opt = getopt(argc, argv, "ch")) != -1) { + switch (opt) { + case 'c': + flags |= bunzip_to_stdout; + break; + case 'h': + default: + show_usage(); /* exit's inside usage */ + } + } + + save_name = xstrdup(argv[optind]); + + if (save_name == NULL) { show_usage(); } - src_stream = xfopen(argv[1], "r"); - save_name = xstrdup(argv[1]); + + src_stream = xfopen(argv[optind], "r"); + save_name_ptr = strrchr(save_name, '.'); if (save_name_ptr == NULL) { return(FALSE); @@ -2333,7 +2358,12 @@ int bunzip2_main(int argc, char **argv) error_msg("Invalid extension, expected .bz2"); } *save_name_ptr = '\0'; - dst_stream = xfopen(save_name, "w"); + + if (flags & bunzip_to_stdout) { + dst_stream = stdout; + } else { + dst_stream = xfopen(save_name, "w"); + } uncompressStream(src_stream, dst_stream); return(TRUE); diff --git a/include/applets.h b/include/applets.h index ea196cb..aa112c3 100644 --- a/include/applets.h +++ b/include/applets.h @@ -68,6 +68,9 @@ APPLET(bunzip2, bunzip2_main, _BB_DIR_USR_BIN) #endif APPLET_NOUSAGE("busybox", busybox_main, _BB_DIR_BIN) +#ifdef CONFIG_BUNZIP2 + APPLET(bzcat, bunzip2_main, _BB_DIR_USR_BIN) +#endif #ifdef CONFIG_CAT APPLET(cat, cat_main, _BB_DIR_BIN) #endif diff --git a/include/usage.h b/include/usage.h index 12d5e1e..df1c18e 100644 --- a/include/usage.h +++ b/include/usage.h @@ -52,11 +52,17 @@ "bar" #define bunzip2_trivial_usage \ - "FILE" + "[-c] FILE" #define bunzip2_full_usage \ "Uncompress FILE to current directory, stripping its .bz2 extension.\n"\ + " -c output to stdout\n"\ " -k is assumed" +#define bzcat_trivial_usage \ + "FILE" +#define bzcat_full_usage \ + "Uncompress to stdout." + #define cat_trivial_usage \ "[FILE]..." #define cat_full_usage \ -- cgit v1.1