summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c161
1 files changed, 65 insertions, 96 deletions
diff --git a/main.c b/main.c
index 97218de..9ba2280 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
/* Lunzip - Decompressor for lzip files
- Copyright (C) 2010, 2011, 2012 Antonio Diaz Diaz.
+ Copyright (C) 2010, 2011, 2012, 2013 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -58,12 +58,10 @@
#error "Environments where CHAR_BIT != 8 are not supported."
#endif
-long long int llabs( long long int number );
-
const char * const Program_name = "Lunzip";
const char * const program_name = "lunzip";
-const char * const program_year = "2012";
+const char * const program_year = "2013";
const char * invocation_name = 0;
#ifdef O_BINARY
@@ -86,21 +84,7 @@ mode_t outfd_mode = S_IRUSR | S_IWUSR;
bool delete_output_on_interrupt = false;
-/* assure at least a minimum size for buffer 'buf' */
-static void * resize_buffer( void * buf, const int min_size )
- {
- if( buf ) buf = realloc( buf, min_size );
- else buf = malloc( min_size );
- if( !buf )
- {
- show_error( "Not enough memory.", 0, false );
- cleanup_and_fail( 1 );
- }
- return buf;
- }
-
-
-static void show_help()
+static void show_help( void )
{
printf( "%s - Decompressor for lzip files.\n", Program_name );
printf( "\nUsage: %s [options] [files]\n", invocation_name );
@@ -122,7 +106,7 @@ static void show_help()
}
-static void show_version()
+static void show_version( void )
{
printf( "%s %s\n", Program_name, PROGVERSION );
printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year );
@@ -132,21 +116,35 @@ static void show_version()
}
-static const char * format_num( long long num )
+void show_header( const File_header header )
{
const char * const prefix[8] =
{ "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
- enum { buf_size = 16, factor = 1024 };
- static char buf[buf_size];
- const char *p = "";
+ enum { factor = 1024 };
+ const char * p = "";
+ const char * np = " ";
+ unsigned num = Fh_get_dictionary_size( header ), i;
bool exact = ( num % factor == 0 );
- int i;
- for( i = 0; i < 8 && ( llabs( num ) > 9999 ||
- ( exact && llabs( num ) >= factor ) ); ++i )
- { num /= factor; if( num % factor != 0 ) exact = false; p = prefix[i]; }
- snprintf( buf, buf_size, "%lld %s", num, p );
- return buf;
+ for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i )
+ { num /= factor; if( num % factor != 0 ) exact = false;
+ p = prefix[i]; np = ""; }
+ fprintf( stderr, "version %d, dictionary size %s%4u %sB. ",
+ Fh_version( header ), np, num, p );
+ }
+
+
+static int extension_index( const char * const name )
+ {
+ int i;
+ for( i = 0; known_extensions[i].from; ++i )
+ {
+ const char * const ext = known_extensions[i].from;
+ if( strlen( name ) > strlen( ext ) &&
+ strncmp( name + strlen( name ) - strlen( ext ), ext, strlen( ext ) ) == 0 )
+ return i;
+ }
+ return -1;
}
@@ -183,23 +181,22 @@ static int open_instream( const char * const name,
}
-static int extension_index( const char * const name )
+/* assure at least a minimum size for buffer 'buf' */
+static void * resize_buffer( void * buf, const int min_size )
{
- int i;
- for( i = 0; known_extensions[i].from; ++i )
+ if( buf ) buf = realloc( buf, min_size );
+ else buf = malloc( min_size );
+ if( !buf )
{
- const char * const ext = known_extensions[i].from;
- if( strlen( name ) > strlen( ext ) &&
- strncmp( name + strlen( name ) - strlen( ext ), ext, strlen( ext ) ) == 0 )
- return i;
+ show_error( "Not enough memory.", 0, false );
+ cleanup_and_fail( 1 );
}
- return -1;
+ return buf;
}
-static void set_d_outname( const char * const name )
+static void set_d_outname( const char * const name, const int i )
{
- const int i = extension_index( name );
if( i >= 0 )
{
const char * const from = known_extensions[i].from;
@@ -286,9 +283,9 @@ static void close_and_set_permissions( const struct stat * const in_statsp )
static int decompress( const int infd, struct Pretty_print * const pp,
const bool testing )
{
- long long partial_file_pos = 0;
+ unsigned long long partial_file_pos = 0;
struct Range_decoder rdec;
- int retval = 0, result;
+ int retval = 0;
bool first_member;
if( !Rd_init( &rdec, infd ) )
{
@@ -296,13 +293,14 @@ static int decompress( const int infd, struct Pretty_print * const pp,
cleanup_and_fail( 1 );
}
- for( first_member = true; ; first_member = false, Pp_reset( pp ) )
+ for( first_member = true; ; first_member = false )
{
+ int result;
File_header header;
struct LZ_decoder decoder;
Rd_reset_member_position( &rdec );
Rd_read_data( &rdec, header, Fh_size );
- if( Rd_finished( &rdec ) ) /* End Of File */
+ if( Rd_finished( &rdec ) ) /* End Of File */
{
if( first_member )
{ Pp_show_msg( pp, "Error reading member header" ); retval = 1; }
@@ -328,13 +326,7 @@ static int decompress( const int infd, struct Pretty_print * const pp,
retval = 2; break; }
if( verbosity >= 2 || ( verbosity == 1 && first_member ) )
- {
- Pp_show_msg( pp, 0 );
- if( verbosity >= 2 )
- fprintf( stderr, "version %d, dictionary size %7sB. ",
- Fh_version( header ),
- format_num( Fh_get_dictionary_size( header ) ) );
- }
+ { Pp_show_msg( pp, 0 ); if( verbosity >= 2 ) show_header( header ); }
if( !LZd_init( &decoder, header, &rdec, outfd ) )
{
@@ -350,16 +342,16 @@ static int decompress( const int infd, struct Pretty_print * const pp,
{
Pp_show_msg( pp, 0 );
if( result == 2 )
- fprintf( stderr, "File ends unexpectedly at pos %lld\n",
+ fprintf( stderr, "File ends unexpectedly at pos %llu\n",
partial_file_pos );
else
- fprintf( stderr, "Decoder error at pos %lld\n", partial_file_pos );
+ fprintf( stderr, "Decoder error at pos %llu\n", partial_file_pos );
}
retval = 2; break;
}
if( verbosity >= 2 )
{ if( testing ) fprintf( stderr, "ok\n" );
- else fprintf( stderr, "done\n" ); }
+ else fprintf( stderr, "done\n" ); Pp_reset( pp ); }
}
Rd_free( &rdec );
if( verbosity == 1 && retval == 0 )
@@ -371,13 +363,13 @@ static int decompress( const int infd, struct Pretty_print * const pp,
void signal_handler( int sig )
{
- sig = 0; /* keep compiler happy */
+ if( sig ) {} /* keep compiler happy */
show_error( "Control-C or similar caught, quitting.", 0, false );
cleanup_and_fail( 1 );
}
-static void set_signals()
+static void set_signals( void )
{
signal( SIGHUP, signal_handler );
signal( SIGINT, signal_handler );
@@ -388,7 +380,7 @@ static void set_signals()
void Pp_init( struct Pretty_print * const pp, const char * const filenames[],
const int num_filenames, const int v )
{
- unsigned int stdin_name_len;
+ unsigned stdin_name_len;
int i;
pp->name = 0;
pp->stdin_name = "(stdin)";
@@ -400,31 +392,13 @@ void Pp_init( struct Pretty_print * const pp, const char * const filenames[],
for( i = 0; i < num_filenames; ++i )
{
const char * const s = filenames[i];
- const int len = ( !strcmp( s, "-" ) ? stdin_name_len : strlen( s ) );
+ const int len = ( (strcmp( s, "-" ) == 0) ? stdin_name_len : strlen( s ) );
if( len > pp->longest_name ) pp->longest_name = len;
}
if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len;
}
-void Pp_show_msg( struct Pretty_print * const pp, const char * const msg )
- {
- if( verbosity >= 0 )
- {
- if( pp->first_post )
- {
- int i, len;
- pp->first_post = false;
- fprintf( stderr, " %s: ", pp->name );
- len = pp->longest_name - strlen( pp->name );
- for( i = 0; i < len; ++i ) fprintf( stderr, " " );
- if( !msg ) fflush( stderr );
- }
- if( msg ) fprintf( stderr, "%s.\n", msg );
- }
- }
-
-
void show_error( const char * const msg, const int errcode, const bool help )
{
if( verbosity >= 0 )
@@ -435,7 +409,7 @@ void show_error( const char * const msg, const int errcode, const bool help )
if( errcode > 0 ) fprintf( stderr, ": %s", strerror( errcode ) );
fprintf( stderr, "\n" );
}
- if( help && invocation_name && invocation_name[0] )
+ if( help )
fprintf( stderr, "Try '%s --help' for more information.\n",
invocation_name );
}
@@ -495,7 +469,7 @@ int main( const int argc, const char * const argv[] )
{
const int code = ap_code( &parser, argind );
const char * const arg = ap_argument( &parser, argind );
- if( !code ) break; /* no more options */
+ if( !code ) break; /* no more options */
switch( code )
{
case 'c': to_stdout = true; break;
@@ -513,28 +487,23 @@ int main( const int argc, const char * const argv[] )
} /* end process options */
#if defined(__MSVCRT__) || defined(__OS2__)
- _fsetmode( stdin, "b" );
- _fsetmode( stdout, "b" );
+ setmode( STDIN_FILENO, O_BINARY );
+ setmode( STDOUT_FILENO, O_BINARY );
#endif
if( testing )
outfd = -1;
- for( ; argind < ap_arguments( &parser ); ++argind )
- {
- if( strcmp( ap_argument( &parser, argind ), "-" ) )
- filenames_given = true;
- ++num_filenames;
- filenames = resize_buffer( filenames, num_filenames * sizeof (char *) );
- filenames[num_filenames-1] = ap_argument( &parser, argind );
- }
+ num_filenames = max( 1, ap_arguments( &parser ) - argind );
+ filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] );
+ filenames[0] = "-";
- if( num_filenames == 0 )
+ for( i = 0; argind + i < ap_arguments( &parser ); ++i )
{
- ++num_filenames;
- filenames = resize_buffer( filenames, sizeof (char *) );
- filenames[num_filenames-1] = "-";
+ filenames[i] = ap_argument( &parser, argind + i );
+ if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true;
}
+
if( !to_stdout && !testing &&
( filenames_given || default_output_filename[0] ) )
set_signals();
@@ -549,7 +518,7 @@ int main( const int argc, const char * const argv[] )
const struct stat * in_statsp;
output_filename[0] = 0;
- if( !filenames[i][0] || !strcmp( filenames[i], "-" ) )
+ if( !filenames[i][0] || strcmp( filenames[i], "-" ) == 0 )
{
input_filename = "";
infd = STDIN_FILENO;
@@ -565,7 +534,7 @@ int main( const int argc, const char * const argv[] )
outfd_mode = all_rw;
if( !open_outstream( force ) )
{
- if( outfd == -1 && retval < 1 ) retval = 1;
+ if( retval < 1 ) retval = 1;
close( infd ); infd = -1;
continue;
}
@@ -582,11 +551,11 @@ int main( const int argc, const char * const argv[] )
if( to_stdout ) outfd = STDOUT_FILENO;
else
{
- set_d_outname( input_filename );
+ set_d_outname( input_filename, extension_index( input_filename ) );
outfd_mode = usr_rw;
if( !open_outstream( force ) )
{
- if( outfd == -1 && retval < 1 ) retval = 1;
+ if( retval < 1 ) retval = 1;
close( infd ); infd = -1;
continue;
}