diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 123 |
1 files changed, 72 insertions, 51 deletions
@@ -1,5 +1,5 @@ /* Clzip - LZMA lossless data compressor - Copyright (C) 2010-2018 Antonio Diaz Diaz. + Copyright (C) 2010-2019 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 @@ -36,20 +36,25 @@ #include <unistd.h> #include <utime.h> #include <sys/stat.h> -#if defined(__MSVCRT__) +#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__) #include <io.h> +#if defined(__MSVCRT__) #define fchmod(x,y) 0 #define fchown(x,y,z) 0 #define strtoull strtoul #define SIGHUP SIGTERM #define S_ISSOCK(x) 0 +#ifndef S_IRGRP #define S_IRGRP 0 #define S_IWGRP 0 #define S_IROTH 0 #define S_IWOTH 0 #endif -#if defined(__OS2__) -#include <io.h> +#endif +#if defined(__DJGPP__) +#define S_ISSOCK(x) 0 +#define S_ISVTX 0 +#endif #endif #include "carg_parser.h" @@ -69,9 +74,8 @@ int verbosity = 0; -const char * const Program_name = "Clzip"; const char * const program_name = "clzip"; -const char * const program_year = "2018"; +const char * const program_year = "2019"; const char * invocation_name = 0; const struct { const char * from; const char * to; } known_extensions[] = { @@ -87,6 +91,8 @@ struct Lzma_options enum Mode { m_compress, m_decompress, m_list, m_test }; +/* Variables used in signal handler context. + They are not declared volatile because the handler never returns. */ char * output_filename = 0; int outfd = -1; bool delete_output_on_interrupt = false; @@ -94,8 +100,18 @@ bool delete_output_on_interrupt = false; static void show_help( void ) { - printf( "%s - LZMA lossless data compressor.\n", Program_name ); - printf( "\nUsage: %s [options] [files]\n", invocation_name ); + printf( "Clzip is a C language version of lzip, fully compatible with lzip 1.4 or\n" + "newer. As clzip is written in C, it may be easier to integrate in\n" + "applications like package managers, embedded devices, or systems lacking\n" + "a C++ compiler.\n" + "\nLzip is a lossless data compressor with a user interface similar to the\n" + "one of gzip or bzip2. Lzip can compress about as fast as gzip (lzip -0)\n" + "or compress most files more than bzip2 (lzip -9). Decompression speed is\n" + "intermediate between gzip and bzip2. Lzip is better than gzip and bzip2\n" + "from a data recovery perspective. Lzip has been designed, written and\n" + "tested with great care to replace gzip and bzip2 as the standard\n" + "general-purpose compressed format for unix-like systems.\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" @@ -111,7 +127,7 @@ static void show_help( void ) " -o, --output=<file> if reading standard input, write to <file>\n" " -q, --quiet suppress all messages\n" " -s, --dictionary-size=<bytes> set dictionary size limit in bytes [8 MiB]\n" - " -S, --volume-size=<bytes> set volume size limit in bytes, implies -k\n" + " -S, --volume-size=<bytes> set volume size limit in bytes\n" " -t, --test test compressed file integrity\n" " -v, --verbose be verbose (a 2nd -v gives more)\n" " -0 .. -9 set compression level [default 6]\n" @@ -227,7 +243,7 @@ static unsigned long long getnum( const char * const ptr, if( !errno && tail[0] ) { const unsigned factor = ( tail[1] == 'i' ) ? 1024 : 1000; - int exponent = 0; /* 0 = bad multiplier */ + int exponent = 0; /* 0 = bad multiplier */ int i; switch( tail[0] ) { @@ -268,7 +284,7 @@ static int get_dict_size( const char * const arg ) const long bits = strtol( arg, &tail, 0 ); if( bits >= min_dictionary_bits && bits <= max_dictionary_bits && *tail == 0 ) - return ( 1 << bits ); + return 1 << bits; return getnum( arg, min_dictionary_size, max_dictionary_size ); } @@ -423,8 +439,17 @@ static bool check_tty( const char * const input_filename, const int infd, } +static void set_signals( void (*action)(int) ) + { + signal( SIGHUP, action ); + signal( SIGINT, action ); + signal( SIGTERM, action ); + } + + void cleanup_and_fail( const int retval ) { + set_signals( SIG_IGN ); /* ignore signals */ if( delete_output_on_interrupt ) { delete_output_on_interrupt = false; @@ -439,6 +464,14 @@ void cleanup_and_fail( const int retval ) } +void signal_handler( int sig ) + { + if( sig ) {} /* keep compiler happy */ + show_error( "Control-C or similar caught, quitting.", 0, false ); + cleanup_and_fail( 1 ); + } + + /* Set permissions, owner and times. */ static void close_and_set_permissions( const struct stat * const in_statsp ) { @@ -518,13 +551,13 @@ static int compress( const unsigned long long cfile_size, } else { - File_header header; - if( Fh_set_dictionary_size( header, encoder_options->dictionary_size ) && + Lzip_header header; + if( Lh_set_dictionary_size( header, encoder_options->dictionary_size ) && encoder_options->match_len_limit >= min_match_len_limit && encoder_options->match_len_limit <= max_match_len ) encoder.e = (struct LZ_encoder *)malloc( sizeof *encoder.e ); else internal_error( "invalid argument to encoder." ); - if( !encoder.e || !LZe_init( encoder.e, Fh_get_dictionary_size( header ), + if( !encoder.e || !LZe_init( encoder.e, Lh_get_dictionary_size( header ), encoder_options->match_len_limit, infd, outfd ) ) error = true; else encoder.eb = &encoder.e->eb; @@ -637,16 +670,16 @@ static int decompress( const unsigned long long cfile_size, const int infd, { int result, size; unsigned dictionary_size; - File_header header; + Lzip_header header; struct LZ_decoder decoder; Rd_reset_member_position( &rdec ); - size = Rd_read_data( &rdec, header, Fh_size ); + size = Rd_read_data( &rdec, header, Lh_size ); if( Rd_finished( &rdec ) ) /* End Of File */ { if( first_member ) { show_file_error( pp->name, "File ends unexpectedly at member header.", 0 ); retval = 2; } - else if( Fh_verify_prefix( header, size ) ) + else if( Lh_verify_prefix( header, size ) ) { Pp_show_msg( pp, "Truncated header in multimember file." ); show_trailing_data( header, size, pp, true, -1 ); retval = 2; } @@ -655,11 +688,11 @@ static int decompress( const unsigned long long cfile_size, const int infd, retval = 2; break; } - if( !Fh_verify_magic( header ) ) + if( !Lh_verify_magic( header ) ) { if( first_member ) { show_file_error( pp->name, bad_magic_msg, 0 ); retval = 2; } - else if( !loose_trailing && Fh_verify_corrupt( header ) ) + else if( !loose_trailing && Lh_verify_corrupt( header ) ) { Pp_show_msg( pp, corrupt_mm_msg ); show_trailing_data( header, size, pp, false, -1 ); retval = 2; } @@ -667,10 +700,10 @@ static int decompress( const unsigned long long cfile_size, const int infd, retval = 2; break; } - if( !Fh_verify_version( header ) ) - { Pp_show_msg( pp, bad_version( Fh_version( header ) ) ); + if( !Lh_verify_version( header ) ) + { Pp_show_msg( pp, bad_version( Lh_version( header ) ) ); retval = 2; break; } - dictionary_size = Fh_get_dictionary_size( header ); + dictionary_size = Lh_get_dictionary_size( header ); if( !isvalid_ds( dictionary_size ) ) { Pp_show_msg( pp, bad_dict_msg ); retval = 2; break; } @@ -689,7 +722,8 @@ static int decompress( const unsigned long long cfile_size, const int infd, { Pp_show_msg( pp, 0 ); fprintf( stderr, "%s at pos %llu\n", ( result == 2 ) ? - "File ends unexpectedly" : "Decoder error", partial_file_pos ); + "File ends unexpectedly" : "Decoder error", + partial_file_pos ); } retval = 2; break; } @@ -703,31 +737,13 @@ static int decompress( const unsigned long long cfile_size, const int infd, } -void signal_handler( int sig ) - { - if( sig ) {} /* keep compiler happy */ - show_error( "Control-C or similar caught, quitting.", 0, false ); - cleanup_and_fail( 1 ); - } - - -static void set_signals( void ) - { - signal( SIGHUP, signal_handler ); - signal( SIGINT, signal_handler ); - signal( SIGTERM, signal_handler ); - } - - void show_error( const char * const msg, const int errcode, const bool help ) { if( verbosity < 0 ) return; if( msg && msg[0] ) - { - fprintf( stderr, "%s: %s", program_name, msg ); - if( errcode > 0 ) fprintf( stderr, ": %s", strerror( errcode ) ); - fputc( '\n', stderr ); - } + fprintf( stderr, "%s: %s%s%s\n", program_name, msg, + ( errcode > 0 ) ? ": " : "", + ( errcode > 0 ) ? strerror( errcode ) : "" ); if( help ) fprintf( stderr, "Try '%s --help' for more information.\n", invocation_name ); @@ -737,10 +753,10 @@ void show_error( const char * const msg, const int errcode, const bool help ) void show_file_error( const char * const filename, const char * const msg, const int errcode ) { - if( verbosity < 0 ) return; - fprintf( stderr, "%s: %s: %s", program_name, filename, msg ); - if( errcode > 0 ) fprintf( stderr, ": %s", strerror( errcode ) ); - fputc( '\n', stderr ); + if( verbosity >= 0 ) + fprintf( stderr, "%s: %s: %s%s%s\n", program_name, filename, msg, + ( errcode > 0 ) ? ": " : "", + ( errcode > 0 ) ? strerror( errcode ) : "" ); } @@ -933,7 +949,7 @@ int main( const int argc, const char * const argv[] ) } } /* end process options */ -#if defined(__MSVCRT__) || defined(__OS2__) +#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__) setmode( STDIN_FILENO, O_BINARY ); setmode( STDOUT_FILENO, O_BINARY ); #endif @@ -961,7 +977,7 @@ int main( const int argc, const char * const argv[] ) if( !to_stdout && program_mode != m_test && ( filenames_given || default_output_filename[0] ) ) - set_signals(); + set_signals( signal_handler ); Pp_init( &pp, filenames, num_filenames ); @@ -1044,6 +1060,12 @@ int main( const int argc, const char * const argv[] ) else tmp = decompress( cfile_size, infd, &pp, ignore_trailing, loose_trailing, program_mode == m_test ); + if( close( infd ) != 0 ) + { + show_error( input_filename[0] ? "Error closing input file" : + "Error closing stdin", errno, false ); + if( tmp < 1 ) tmp = 1; + } if( tmp > retval ) retval = tmp; if( tmp ) { if( program_mode != m_test ) cleanup_and_fail( retval ); @@ -1053,7 +1075,6 @@ int main( const int argc, const char * const argv[] ) close_and_set_permissions( in_statsp ); if( input_filename[0] ) { - close( infd ); if( !keep_input_files && !to_stdout && program_mode != m_test && ( program_mode != m_compress || volume_size == 0 ) ) remove( input_filename ); |