From 92137db2ab511725eaff88fd53eca5287b40f556 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 7 Nov 2015 06:10:11 +0100 Subject: Merging upstream version 1.8~pre1. Signed-off-by: Daniel Baumann --- main.c | 60 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 26 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 59789ad..0b220da 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -/* Lunzip - Decompressor for lzip files +/* Lunzip - Decompressor for the lzip format Copyright (C) 2010-2015 Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify @@ -84,7 +84,7 @@ bool delete_output_on_interrupt = false; static void show_help( void ) { - printf( "Lunzip is a decompressor for lzip files. It is written in C and its\n" + printf( "Lunzip is a decompressor for the lzip format. It is written in C and its\n" "small size makes it well suited for embedded devices or software\n" "installers that need to decompress files but do not need compression\n" "capabilities. Lunzip is fully compatible with lzip-1.4 or newer.\n" @@ -95,8 +95,8 @@ static void show_help( void ) "file as dictionary for distances beyond the buffer size. Of course, the\n" "smaller the output buffer size used in relation to the dictionary size,\n" "the more accesses to disk are needed and the slower the decompression is.\n" - "This 'low memory' mode only works when decompressing to a regular file.\n" ); - printf( "\nUsage: %s [options] [files]\n", invocation_name ); + "This 'low memory' mode only works when decompressing to a regular file.\n" + "\nUsage: %s [options] [files]\n", invocation_name ); printf( "\nOptions:\n" " -h, --help display this help and exit\n" " -V, --version output version information and exit\n" @@ -242,7 +242,7 @@ static int open_instream( const char * const name, struct stat * const in_statsp if( infd < 0 ) { if( verbosity >= 0 ) - fprintf( stderr, "%s: Can't open input file '%s': %s.\n", + fprintf( stderr, "%s: Can't open input file '%s': %s\n", program_name, name, strerror( errno ) ); } else @@ -258,7 +258,7 @@ static int open_instream( const char * const name, struct stat * const in_statsp fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n", program_name, name, ( can_read && !no_ofile ) ? - " and '--stdout' was not specified" : "" ); + ",\n and '--stdout' was not specified" : "" ); close( infd ); infd = -1; } @@ -301,7 +301,7 @@ static void set_d_outname( const char * const name, const int i ) strcpy( output_filename, name ); strcat( output_filename, ".out" ); if( verbosity >= 1 ) - fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'.\n", + fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n", program_name, name, output_filename ); } @@ -318,7 +318,7 @@ static bool open_outstream( const bool force ) fprintf( stderr, "%s: Output file '%s' already exists, skipping.\n", program_name, output_filename ); else - fprintf( stderr, "%s: Can't create output file '%s': %s.\n", + fprintf( stderr, "%s: Can't create output file '%s': %s\n", program_name, output_filename, strerror( errno ) ); } return ( outfd >= 0 ); @@ -371,8 +371,8 @@ static void close_and_set_permissions( const struct stat * const in_statsp ) } -static int decompress( const int buffer_size, const int infd, - struct Pretty_print * const pp, const bool testing ) +static int decompress( const int infd, struct Pretty_print * const pp, + const int buffer_size, const bool testing ) { unsigned long long partial_file_pos = 0; struct Range_decoder rdec; @@ -435,20 +435,17 @@ static int decompress( const int buffer_size, const int infd, if( verbosity >= 0 && result <= 2 ) { Pp_show_msg( pp, 0 ); - if( result == 2 ) - fprintf( stderr, "File ends unexpectedly at pos %llu.\n", - partial_file_pos ); - else - fprintf( stderr, "Decoder error at pos %llu.\n", partial_file_pos ); + fprintf( stderr, "%s at pos %llu\n", ( result == 2 ) ? + "File ends unexpectedly" : "Decoder error", partial_file_pos ); } retval = 2; break; } if( verbosity >= 2 ) - { fprintf( stderr, testing ? "ok\n" : "done\n" ); Pp_reset( pp ); } + { fputs( testing ? "ok\n" : "done\n", stderr ); Pp_reset( pp ); } } Rd_free( &rdec ); if( verbosity == 1 && retval == 0 ) - fprintf( stderr, testing ? "ok\n" : "done\n" ); + fputs( testing ? "ok\n" : "done\n", stderr ); return retval; } @@ -476,8 +473,8 @@ void show_error( const char * const msg, const int errcode, const bool help ) if( msg && msg[0] ) { fprintf( stderr, "%s: %s", program_name, msg ); - if( errcode > 0 ) fprintf( stderr, ": %s.", strerror( errcode ) ); - fprintf( stderr, "\n" ); + if( errcode > 0 ) fprintf( stderr, ": %s", strerror( errcode ) ); + fputc( '\n', stderr ); } if( help ) fprintf( stderr, "Try '%s --help' for more information.\n", @@ -581,15 +578,26 @@ int main( const int argc, const char * const argv[] ) if( buffer_size < max_dictionary_size ) { + struct stat st; + bool from_stdin = false; if( to_stdout || testing ) { show_error( "'--buffer-size' is incompatible with '--stdout' and '--test'.", 0, false ); return 1; } - if( !default_output_filename[0] ) - for( i = 0; i < num_filenames; ++i ) - if( !filenames[i][0] || strcmp( filenames[i], "-" ) == 0 ) - { show_error( "Output file must be specified when decompressing from stdin with a\n" - " reduced buffer size.", 0, false ); - return 1; } + for( i = 0; i < num_filenames; ++i ) + if( !filenames[i][0] || strcmp( filenames[i], "-" ) == 0 ) + { from_stdin = true; break; } + if( from_stdin && !default_output_filename[0] ) + { show_error( "Output file must be specified when decompressing from stdin with a\n" + " reduced buffer size.", 0, false ); return 1; } + if( from_stdin && default_output_filename[0] && + stat( default_output_filename, &st ) == 0 && !S_ISREG( st.st_mode ) ) + { + if( verbosity >= 0 ) + fprintf( stderr, "%s: Output file '%s' is not a regular file,\n" + " and 'low memory' mode has been requested.\n", + program_name, default_output_filename ); + return 1; + } } if( !to_stdout && !testing && @@ -661,7 +669,7 @@ int main( const int argc, const char * const argv[] ) delete_output_on_interrupt = true; in_statsp = input_filename[0] ? &in_stats : 0; Pp_set_name( &pp, input_filename ); - tmp = decompress( buffer_size, infd, &pp, testing ); + tmp = decompress( infd, &pp, buffer_size, testing ); if( tmp > retval ) retval = tmp; if( tmp && !testing ) cleanup_and_fail( retval ); -- cgit v1.2.3