[BusyBox] bunzip2/bzcat STDIN

Per Wigren wigren at home.se
Wed Mar 27 11:47:04 UTC 2002


Thanks a million!
The patch failed for some reason so I'll just wait for anon-cvs to catch up.. 
:)

Thanks again! Does open source rock or what... ;))

Regards
Per Wigren


Wednesday 27 March 2002 19.13 skrev Matt Kraai:
> On Wed, Mar 27, 2002 at 05:50:29PM +0000, Per Wigren wrote:
> > So how do I make it read from STDIN?
> >
> > # cat file.bz2 | busybox bzcat
> > BusyBox v0.61.pre (2002.03.27-11:30+0000) multi-call binary
> >
> > Usage: bzcat FILE
> > # cat file.bz2 | busybox bzcat -
> > bzcat: -: No such file or directory
>
> You wait for the anonymous CVS repository to catch up, or you
> apply the following patch.  :)
>
> Matt
>
> Index: archival/bunzip2.c
> ===================================================================
> RCS file: /var/cvs/busybox/archival/bunzip2.c,v
> retrieving revision 1.5
> diff -u -p -u -p -r1.5 bunzip2.c
> --- archival/bunzip2.c	20 Dec 2001 23:12:45 -0000	1.5
> +++ archival/bunzip2.c	27 Mar 2002 17:22:28 -0000
> @@ -58,6 +58,7 @@
>  #include <stdio.h>
>  #include <string.h>
>  #include <getopt.h>
> +#include <unistd.h>
>  #include <busybox.h>
>
>  //#define TRUE 1
> @@ -2319,45 +2320,49 @@ errhandler_io:
>  int bunzip2_main(int argc, char **argv)
>  {
>  	const int bunzip_to_stdout = 1;
> +	const int bunzip_force = 2;
>  	int flags = 0;
>  	int opt = 0;
>
>  	FILE *src_stream;
>  	FILE *dst_stream;
> -	char *save_name;
> -	char *save_name_ptr;
> +	char *save_name = NULL;
>
>  	/* if called as bzcat */
>  	if (strcmp(applet_name, "bzcat") == 0)
>  		flags |= bunzip_to_stdout;
>
> -	while ((opt = getopt(argc, argv, "ch")) != -1) {
> +	while ((opt = getopt(argc, argv, "cfh")) != -1) {
>  		switch (opt) {
>  		case 'c':
>  			flags |= bunzip_to_stdout;
>  			break;
> +		case 'f':
> +			flags |= bunzip_force;
> +			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[optind], "r");
> +	/* Set input filename and number */
> +	if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
> +		flags |= bunzip_to_stdout;
> +		src_stream = stdin;
> +	} else {
> +		/* Open input file */
> +		src_stream = xfopen(argv[optind], "r");
>
> -	save_name_ptr = strrchr(save_name, '.');
> -	if (save_name_ptr == NULL) {
> -		return(FALSE);
> -	}
> -	if (strcmp(save_name_ptr, ".bz2") != 0) {
> -		error_msg("Invalid extension, expected .bz2");
> +		save_name = xstrdup(argv[optind]);
> +		if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
> +			error_msg_and_die("Invalid extension");
> +		save_name[strlen(save_name) - 4] = '\0';
>  	}
> -	*save_name_ptr = '\0';
> +
> +	/* Check that the input is sane.  */
> +	if (isatty(fileno(src_stream)) && (flags & bunzip_force) == 0)
> +		error_msg_and_die("compressed data not read from terminal.  Use -f to
> force it.");
>
>  	if (flags & bunzip_to_stdout) {
>  		dst_stream = stdout;
> Index: include/usage.h
> ===================================================================
> RCS file: /var/cvs/busybox/include/usage.h,v
> retrieving revision 1.84
> diff -u -p -u -p -r1.84 usage.h
> --- include/usage.h	20 Mar 2002 17:38:36 -0000	1.84
> +++ include/usage.h	27 Mar 2002 17:22:29 -0000
> @@ -52,11 +52,12 @@
>  	"bar"
>
>  #define bunzip2_trivial_usage \
> -	"[-c] FILE"
> +	"[OPTION]... [FILE]"
>  #define bunzip2_full_usage \
> -	"Uncompress FILE to current directory, stripping its .bz2 extension.\n"\
> -	" -c output to stdout\n"\
> -	" -k is assumed"
> +	"Uncompress FILE (or standard input if FILE is '-' or omitted).\n\n" \
> +	"Options:\n" \
> +	"\t-c\tWrite output to standard output\n" \
> +	"\t-f\tForce"
>
>  #define bzcat_trivial_usage \
>  	"FILE"




More information about the busybox mailing list