diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 14:08:01 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 14:08:29 +0000 |
commit | de5f20df22be951183a277d2fb3d01883550e28e (patch) | |
tree | db0d8cb002b17d21c517b183ce91c072b8744474 /main.c | |
parent | Releasing debian version 1.10-9. (diff) | |
download | pdlzip-de5f20df22be951183a277d2fb3d01883550e28e.tar.xz pdlzip-de5f20df22be951183a277d2fb3d01883550e28e.zip |
Merging upstream version 1.11.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 450 |
1 files changed, 253 insertions, 197 deletions
@@ -1,27 +1,27 @@ -/* Pdlzip - LZMA lossless data compressor - 2009-08-14 : Igor Pavlov : Public domain - Copyright (C) 2010-2019 Antonio Diaz Diaz. +/* Pdlzip - LZMA lossless data compressor + 2009-08-14 : Igor Pavlov : Public domain + Copyright (C) 2010-2021 Antonio Diaz Diaz. - This program is free software. Redistribution and use in source and - binary forms, with or without modification, are permitted provided - that the following conditions are met: + This program is free software. Redistribution and use in source and + binary forms, with or without modification, are permitted provided + that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions, and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions, and the following disclaimer in the + documentation and/or other materials provided with the distribution. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ /* - Exit status: 0 for a normal exit, 1 for environmental problems - (file not found, invalid flags, I/O errors, etc), 2 to indicate a - corrupt or invalid input file, 3 for an internal consistency error - (eg, bug) which caused pdlzip to panic. + Exit status: 0 for a normal exit, 1 for environmental problems + (file not found, invalid flags, I/O errors, etc), 2 to indicate a + corrupt or invalid input file, 3 for an internal consistency error + (eg, bug) which caused pdlzip to panic. */ #define _FILE_OFFSET_BITS 64 @@ -72,12 +72,18 @@ #endif int verbosity = 0; - -const char * const program_name = "pdlzip"; -const char * const program_year = "2019"; -const char * invocation_name = 0; - -const struct { const char * from; const char * to; } known_extensions[] = { +static void cleanup_and_fail( const int retval ); +static void show_error( const char * const msg, const int errcode, + const bool help ); +static void show_file_error( const char * const filename, + const char * const msg, const int errcode ); +static void internal_error( const char * const msg ); + +static const char * const program_name = "pdlzip"; +static const char * const program_year = "2021"; +static const char * invocation_name = "pdlzip"; /* default value */ + +static const struct { const char * from; const char * to; } known_extensions[] = { { ".lz", "" }, { ".tlz", ".tar" }, { ".lzma", "" }, @@ -93,9 +99,9 @@ enum Mode { m_compress, m_decompress, 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; +static char * output_filename = 0; +static int outfd = -1; +static bool delete_output_on_interrupt = false; static void show_help( void ) @@ -105,13 +111,15 @@ static void show_help( void ) "licensed Free Software. (The name of pdlzip comes from 'public domain\n" "lzip'). Pdlzip is written in C and is (hope)fully compatible with lzip 1.4\n" "or newer.\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" + "\nLzip is a lossless data compressor with a user interface similar to the one\n" + "of gzip or bzip2. Lzip uses a simplified form of the 'Lempel-Ziv-Markov\n" + "chain-Algorithm' (LZMA) stream format, chosen to maximize safety and\n" + "interoperability. Lzip can compress about as fast as gzip (lzip -0) or\n" + "compress most files more than bzip2 (lzip -9). Decompression speed is\n" + "intermediate between gzip and bzip2. Lzip is better than gzip and bzip2 from\n" + "a data recovery perspective. Lzip has been designed, written, and tested\n" + "with great care to replace gzip and bzip2 as the standard general-purpose\n" + "compressed format for unix-like systems.\n" "\nPdlzip is also able to decompress legacy lzma-alone (.lzma) files.\n" "Lzma-alone is a very bad format; it is essentially a raw LZMA stream.\n" "If you keep any lzma-alone files, it is advisable to recompress them to\n" @@ -128,7 +136,7 @@ static void show_help( void ) " -F, --recompress force re-compression of compressed files\n" " -k, --keep keep (don't delete) input files\n" " -m, --match-length=<bytes> set match length limit in bytes [36]\n" - " -o, --output=<file> if reading standard input, write to <file>\n" + " -o, --output=<file> write to <file>, keep input files\n" " -q, --quiet suppress all messages\n" " -s, --dictionary-size=<bytes> set dictionary size limit in bytes [8 MiB]\n" " -t, --test test compressed file integrity\n" @@ -137,7 +145,7 @@ static void show_help( void ) " --fast alias for -0\n" " --best alias for -9\n" " --loose-trailing allow trailing data seeming corrupt header\n" - "If no file names are given, or if a file is '-', pdlzip compresses or\n" + "\nIf no file names are given, or if a file is '-', pdlzip compresses or\n" "decompresses from standard input to standard output.\n" "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n" "Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" @@ -145,13 +153,17 @@ static void show_help( void ) "to 2^27 bytes.\n" "\nThe bidimensional parameter space of LZMA can't be mapped to a linear\n" "scale optimal for all files. If your files are large, very repetitive,\n" - "etc, you may need to use the --dictionary-size and --match-length\n" - "options directly to achieve optimal performance. For example, -9m64\n" - "usually compresses executables more (and faster) than -9.\n" + "etc, you may need to use the options --dictionary-size and --match-length\n" + "directly to achieve optimal performance. For example, -9m64 usually\n" + "compresses executables more (and faster) than -9.\n" + "\nTo extract all the files from archive 'foo.tar.lz', use the commands\n" + "'tar -xf foo.tar.lz' or 'pdlzip -cd foo.tar.lz | tar -xf -'.\n" "\nExit status: 0 for a normal exit, 1 for environmental problems (file\n" "not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or\n" "invalid input file, 3 for an internal consistency error (eg, bug) which\n" "caused pdlzip to panic.\n" + "\nPdlzip includes public domain compression/decompression code from the LZMA\n" + "SDK (Software Development Kit) written by Igor Pavlov.\n" "\nReport bugs to lzip-bug@nongnu.org\n" "Pdlzip home page: http://www.nongnu.org/lzip/pdlzip.html\n" ); } @@ -169,20 +181,69 @@ static void show_version( void ) /* assure at least a minimum size for buffer 'buf' */ -void * resize_buffer( void * buf, const unsigned min_size ) +static void * resize_buffer( void * buf, const unsigned min_size ) { if( buf ) buf = realloc( buf, min_size ); else buf = malloc( min_size ); - if( !buf ) + if( !buf ) { show_error( mem_msg, 0, false ); cleanup_and_fail( 1 ); } + return buf; + } + + +struct Pretty_print + { + const char * name; + char * padded_name; + const char * stdin_name; + unsigned longest_name; + bool first_post; + }; + +static void Pp_init( struct Pretty_print * const pp, + const char * const filenames[], const int num_filenames ) + { + unsigned stdin_name_len; + int i; + pp->name = 0; + pp->padded_name = 0; + pp->stdin_name = "(stdin)"; + pp->longest_name = 0; + pp->first_post = false; + + if( verbosity <= 0 ) return; + stdin_name_len = strlen( pp->stdin_name ); + for( i = 0; i < num_filenames; ++i ) { - show_error( "Not enough memory.", 0, false ); - cleanup_and_fail( 1 ); + const char * const s = filenames[i]; + const unsigned len = (strcmp( s, "-" ) == 0) ? stdin_name_len : strlen( s ); + if( pp->longest_name < len ) pp->longest_name = len; } - return buf; + if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len; } +static void Pp_set_name( struct Pretty_print * const pp, + const char * const filename ) + { + unsigned name_len, padded_name_len, i = 0; + + if( filename && filename[0] && strcmp( filename, "-" ) != 0 ) + pp->name = filename; + else pp->name = pp->stdin_name; + name_len = strlen( pp->name ); + padded_name_len = max( name_len, pp->longest_name ) + 4; + pp->padded_name = resize_buffer( pp->padded_name, padded_name_len + 1 ); + while( i < 2 ) pp->padded_name[i++] = ' '; + while( i < name_len + 2 ) { pp->padded_name[i] = pp->name[i-2]; ++i; } + pp->padded_name[i++] = ':'; + while( i < padded_name_len ) pp->padded_name[i++] = ' '; + pp->padded_name[i] = 0; + pp->first_post = true; + } -void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) +static void Pp_reset( struct Pretty_print * const pp ) + { if( pp->name && pp->name[0] ) pp->first_post = true; } + +static void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) { if( verbosity >= 0 ) { @@ -210,7 +271,7 @@ static void show_header( const unsigned dictionary_size ) int i; 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, "dictionary %s%4u %sB, ", np, num, p ); + fprintf( stderr, "dict %s%4u %sB, ", np, num, p ); } @@ -277,6 +338,17 @@ static int get_dict_size( const char * const arg ) } +static void set_mode( enum Mode * const program_modep, const enum Mode new_mode ) + { + if( *program_modep != m_compress && *program_modep != new_mode ) + { + show_error( "Only one operation can be specified.", 0, true ); + exit( 1 ); + } + *program_modep = new_mode; + } + + static int extension_index( const char * const name ) { int eindex; @@ -293,12 +365,16 @@ static int extension_index( const char * const name ) } -static void set_c_outname( const char * const name, const bool force_ext ) +static void set_c_outname( const char * const name, const bool filenames_given, + const bool force_ext ) { + /* zupdate < 1.9 depends on lzip adding the extension '.lz' to name when + reading from standard input. */ output_filename = resize_buffer( output_filename, strlen( name ) + strlen( known_extensions[0].from ) + 1 ); strcpy( output_filename, name ); - if( force_ext || extension_index( output_filename ) < 0 ) + if( force_ext || + ( !filenames_given && extension_index( output_filename ) < 0 ) ) strcat( output_filename, known_extensions[0].from ); } @@ -330,7 +406,7 @@ static void set_d_outname( const char * const name, const int eindex ) static int open_instream( const char * const name, struct stat * const in_statsp, const enum Mode program_mode, const int eindex, - const bool recompress, const bool to_stdout ) + const bool one_to_one, const bool recompress ) { int infd = -1; if( program_mode == m_compress && !recompress && eindex >= 0 ) @@ -351,14 +427,12 @@ static int open_instream( const char * const name, struct stat * const in_statsp const bool can_read = ( i == 0 && ( S_ISBLK( mode ) || S_ISCHR( mode ) || S_ISFIFO( mode ) || S_ISSOCK( mode ) ) ); - const bool no_ofile = ( to_stdout || program_mode == m_test ); - if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) ) + if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || one_to_one ) ) ) { if( verbosity >= 0 ) fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n", - program_name, name, - ( can_read && !no_ofile ) ? - ",\n and '--stdout' was not specified" : "" ); + program_name, name, ( can_read && one_to_one ) ? + ",\n and neither '-c' nor '-o' were specified" : "" ); close( infd ); infd = -1; } @@ -368,11 +442,11 @@ static int open_instream( const char * const name, struct stat * const in_statsp } -static bool open_outstream( const bool force, const bool from_stdin ) +static bool open_outstream( const bool force, const bool protect ) { const mode_t usr_rw = S_IRUSR | S_IWUSR; const mode_t all_rw = usr_rw | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; - const mode_t outfd_mode = from_stdin ? all_rw : usr_rw; + const mode_t outfd_mode = protect ? usr_rw : all_rw; int flags = O_CREAT | O_WRONLY | O_BINARY; if( force ) flags |= O_TRUNC; else flags |= O_EXCL; @@ -391,25 +465,6 @@ static bool open_outstream( const bool force, const bool from_stdin ) } -static bool check_tty( const char * const input_filename, const int infd, - const enum Mode program_mode ) - { - if( program_mode == m_compress && isatty( outfd ) ) - { - show_error( "I won't write compressed data to a terminal.", 0, true ); - return false; - } - if( ( program_mode == m_decompress || program_mode == m_test ) && - isatty( infd ) ) - { - show_file_error( input_filename, - "I won't read compressed data from a terminal.", 0 ); - return false; - } - return true; - } - - static void set_signals( void (*action)(int) ) { signal( SIGHUP, action ); @@ -418,7 +473,7 @@ static void set_signals( void (*action)(int) ) } -void cleanup_and_fail( const int retval ) +static void cleanup_and_fail( const int retval ) { set_signals( SIG_IGN ); /* ignore signals */ if( delete_output_on_interrupt ) @@ -435,7 +490,7 @@ void cleanup_and_fail( const int retval ) } -void signal_handler( int sig ) +static void signal_handler( int sig ) { if( sig ) {} /* keep compiler happy */ show_error( "Control-C or similar caught, quitting.", 0, false ); @@ -443,7 +498,31 @@ void signal_handler( int sig ) } - /* Set permissions, owner and times. */ +static bool check_tty_in( const char * const input_filename, const int infd, + const enum Mode program_mode, int * const retval ) + { + if( ( program_mode == m_decompress || program_mode == m_test ) && + isatty( infd ) ) /* for example /dev/tty */ + { show_file_error( input_filename, + "I won't read compressed data from a terminal.", 0 ); + close( infd ); set_retval( retval, 1 ); + if( program_mode != m_test ) cleanup_and_fail( *retval ); + return false; } + return true; + } + +static bool check_tty_out( const enum Mode program_mode ) + { + if( program_mode == m_compress && isatty( outfd ) ) + { show_file_error( output_filename[0] ? + output_filename : "(stdout)", + "I won't write compressed data to a terminal.", 0 ); + return false; } + return true; + } + + +/* Set permissions, owner, and times. */ static void close_and_set_permissions( const struct stat * const in_statsp ) { bool warning = false; @@ -509,6 +588,28 @@ static int compress( const struct Lzma_options * const encoder_options, } +static void show_results( const long long data_size, + const long long member_size, const unsigned crc, + const unsigned dictionary_size, const bool lzip_mode ) + { + if( verbosity >= 2 ) + { + if( verbosity >= 4 ) show_header( dictionary_size ); + if( data_size == 0 || member_size == 0 ) + fputs( "no data compressed. ", stderr ); + else + fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved. ", + (double)data_size / member_size, + ( 100.0 * member_size ) / data_size, + 100.0 - ( ( 100.0 * member_size ) / data_size ) ); + if( verbosity >= 4 && lzip_mode ) fprintf( stderr, "CRC %08X, ", crc ); + if( verbosity >= 3 ) + fprintf( stderr, "%9llu out, %8llu in. ", data_size, member_size ); + if( !lzip_mode ) fputs( "lzma-alone, ", stderr ); + } + } + + #define IN_BUF_SIZE (1 << 16) #define OUT_BUF_SIZE (1 << 16) @@ -584,21 +685,7 @@ static int lzma_decode( uint64_t unpackSize, CLzmaDec *decoder, const int infd, if( ( thereIsSize && unpackSize != 0 ) || ( !thereIsSize && status != LZMA_STATUS_FINISHED_WITH_MARK ) ) { show_error( "Data error.", 0, false ); return 2; } - if( verbosity >= 2 ) - { - if( verbosity >= 4 ) show_header( dictionary_size ); - if( data_size == 0 || member_size == 0 ) - fputs( "no data compressed. ", stderr ); - else - fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved. ", - (double)data_size / member_size, - ( 100.0 * member_size ) / data_size, - 100.0 - ( ( 100.0 * member_size ) / data_size ) ); - if( verbosity >= 3 ) - fprintf( stderr, "decompressed %9llu, compressed %8llu. ", - data_size, member_size ); - fputs( "lzma-alone, ", stderr ); - } + show_results( data_size, member_size, 0, dictionary_size, false ); if( verbosity >= 1 ) fputs( testing ? "(apparently) ok\n" : "(apparently) done\n", stderr ); return 0; @@ -705,21 +792,7 @@ static int lzip_decode( CLzmaDec *decoder, const int infd, } } if( error ) return 2; - if( verbosity >= 2 ) - { - if( verbosity >= 4 ) show_header( dictionary_size ); - if( data_size == 0 || member_size == 0 ) - fputs( "no data compressed. ", stderr ); - else - fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved. ", - (double)data_size / member_size, - ( 100.0 * member_size ) / data_size, - 100.0 - ( ( 100.0 * member_size ) / data_size ) ); - if( verbosity >= 4 ) fprintf( stderr, "CRC %08X, ", td_crc ); - if( verbosity >= 3 ) - fprintf( stderr, "decompressed %9llu, compressed %8llu. ", - data_size, member_size ); - } + show_results( data_size, member_size, td_crc, dictionary_size, true ); return 0; } } @@ -742,7 +815,7 @@ static int decompress( const int infd, struct Pretty_print * const pp, for( first_member = true; ; first_member = false ) { int i, size; - unsigned dictionary_size; + unsigned dictionary_size = 0; /* keep gcc 3.3.6 happy */ Lzip_header header; if( inSize - inPos < lzma_header_size && !read_inbuf( infd, inBuf, &inPos, &inSize ) ) return 1; @@ -819,7 +892,7 @@ static int decompress( const int infd, struct Pretty_print * const pp, Pp_show_msg( pp, 0 ); if( !LzmaDec_Init( &decoder, raw_props ) ) - { Pp_show_msg( pp, "Not enough memory." ); return 1; } + { Pp_show_msg( pp, mem_msg ); return 1; } if( lzip_mode ) retval = lzip_decode( &decoder, infd, pp, inBuf, &inPos, &inSize, dictionary_size ); @@ -877,7 +950,8 @@ int writeblock( const int fd, const uint8_t * const buf, const int size ) } -void show_error( const char * const msg, const int errcode, const bool help ) +static void show_error( const char * const msg, const int errcode, + const bool help ) { if( verbosity < 0 ) return; if( msg && msg[0] ) @@ -890,8 +964,8 @@ 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 ) +static void show_file_error( const char * const filename, + const char * const msg, const int errcode ) { if( verbosity >= 0 ) fprintf( stderr, "%s: %s: %s%s%s\n", program_name, filename, msg, @@ -900,7 +974,7 @@ void show_file_error( const char * const filename, const char * const msg, } -void internal_error( const char * const msg ) +static void internal_error( const char * const msg ) { if( verbosity >= 0 ) fprintf( stderr, "%s: internal error: %s\n", program_name, msg ); @@ -926,7 +1000,9 @@ int main( const int argc, const char * const argv[] ) { 1 << 25, 273 } }; /* -9 */ struct Lzma_options encoder_options = option_mapping[6]; /* default = "-6" */ const char * default_output_filename = ""; - const char ** filenames = 0; + static struct Arg_parser parser; /* static because valgrind complains */ + static struct Pretty_print pp; /* and memory management in C sucks */ + static const char ** filenames = 0; int num_filenames = 0; enum Mode program_mode = m_compress; int argind = 0; @@ -941,20 +1017,19 @@ int main( const int argc, const char * const argv[] ) bool recompress = false; bool stdin_used = false; bool to_stdout = false; - struct Pretty_print pp; enum { opt_lt = 256 }; const struct ap_Option options[] = { { '0', "fast", ap_no }, - { '1', 0, ap_no }, - { '2', 0, ap_no }, - { '3', 0, ap_no }, - { '4', 0, ap_no }, - { '5', 0, ap_no }, - { '6', 0, ap_no }, - { '7', 0, ap_no }, - { '8', 0, ap_no }, + { '1', 0, ap_no }, + { '2', 0, ap_no }, + { '3', 0, ap_no }, + { '4', 0, ap_no }, + { '5', 0, ap_no }, + { '6', 0, ap_no }, + { '7', 0, ap_no }, + { '8', 0, ap_no }, { '9', "best", ap_no }, { 'a', "trailing-error", ap_no }, { 'b', "member-size", ap_yes }, @@ -974,15 +1049,13 @@ int main( const int argc, const char * const argv[] ) { 'v', "verbose", ap_no }, { 'V', "version", ap_no }, { opt_lt, "loose-trailing", ap_no }, - { 0 , 0, ap_no } }; + { 0, 0, ap_no } }; - struct Arg_parser parser; - - invocation_name = argv[0]; + if( argc > 0 ) invocation_name = argv[0]; CRC32_init(); if( !ap_init( &parser, argc, argv, options, 0 ) ) - { show_error( "Not enough memory.", 0, false ); return 1; } + { show_error( mem_msg, 0, false ); return 1; } if( ap_error( &parser ) ) /* bad option */ { show_error( ap_error( &parser ), 0, true ); return 1; } @@ -999,7 +1072,7 @@ int main( const int argc, const char * const argv[] ) case 'a': ignore_trailing = false; break; case 'b': break; case 'c': to_stdout = true; break; - case 'd': program_mode = m_decompress; break; + case 'd': set_mode( &program_mode, m_decompress ); break; case 'f': force = true; break; case 'F': recompress = true; break; case 'h': show_help(); return 0; @@ -1007,12 +1080,13 @@ int main( const int argc, const char * const argv[] ) case 'm': encoder_options.match_len_limit = getnum( arg, min_match_len_limit, max_match_len ); break; case 'n': break; - case 'o': default_output_filename = arg; break; + case 'o': if( strcmp( arg, "-" ) == 0 ) to_stdout = true; + else { default_output_filename = arg; } break; case 'q': verbosity = -1; break; case 's': encoder_options.dictionary_size = get_dict_size( arg ); break; case 'S': break; - case 't': program_mode = m_test; break; + case 't': set_mode( &program_mode, m_test ); break; case 'v': if( verbosity < 4 ) ++verbosity; break; case 'V': show_version(); return 0; case opt_lt: loose_trailing = true; break; @@ -1035,16 +1109,23 @@ int main( const int argc, const char * const argv[] ) if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true; } - if( program_mode == m_test ) - outfd = -1; + if( program_mode == m_test ) to_stdout = false; /* apply overrides */ + if( program_mode == m_test || to_stdout ) default_output_filename = ""; - if( !to_stdout && program_mode != m_test && - ( filenames_given || default_output_filename[0] ) ) + output_filename = resize_buffer( output_filename, 1 ); + output_filename[0] = 0; + if( to_stdout && program_mode != m_test ) /* check tty only once */ + { outfd = STDOUT_FILENO; if( !check_tty_out( program_mode ) ) return 1; } + else outfd = -1; + + const bool to_file = !to_stdout && program_mode != m_test && + default_output_filename[0]; + if( !to_stdout && program_mode != m_test && ( filenames_given || to_file ) ) set_signals( signal_handler ); Pp_init( &pp, filenames, num_filenames ); - output_filename = resize_buffer( output_filename, 1 ); + const bool one_to_one = !to_stdout && program_mode != m_test && !to_file; for( i = 0; i < num_filenames; ++i ) { const char * input_filename = ""; @@ -1052,96 +1133,71 @@ int main( const int argc, const char * const argv[] ) int tmp; struct stat in_stats; const struct stat * in_statsp; - output_filename[0] = 0; - if( !filenames[i][0] || strcmp( filenames[i], "-" ) == 0 ) + Pp_set_name( &pp, filenames[i] ); + if( strcmp( filenames[i], "-" ) == 0 ) { if( stdin_used ) continue; else stdin_used = true; infd = STDIN_FILENO; - if( program_mode != m_test ) - { - if( to_stdout || !default_output_filename[0] ) - outfd = STDOUT_FILENO; - else - { - if( program_mode == m_compress ) - set_c_outname( default_output_filename, false ); - else - { - output_filename = resize_buffer( output_filename, - strlen( default_output_filename ) + 1 ); - strcpy( output_filename, default_output_filename ); - } - if( !open_outstream( force, true ) ) - { - if( retval < 1 ) retval = 1; - close( infd ); - continue; - } - } - } + if( !check_tty_in( pp.name, infd, program_mode, &retval ) ) continue; + if( one_to_one ) { outfd = STDOUT_FILENO; output_filename[0] = 0; } } else { const int eindex = extension_index( input_filename = filenames[i] ); infd = open_instream( input_filename, &in_stats, program_mode, - eindex, recompress, to_stdout ); - if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; } - if( program_mode != m_test ) + eindex, one_to_one, recompress ); + if( infd < 0 ) { set_retval( &retval, 1 ); continue; } + if( !check_tty_in( pp.name, infd, program_mode, &retval ) ) continue; + if( one_to_one ) /* open outfd after verifying infd */ { - if( to_stdout ) outfd = STDOUT_FILENO; - else - { - if( program_mode == m_compress ) - set_c_outname( input_filename, true ); - else set_d_outname( input_filename, eindex ); - if( !open_outstream( force, false ) ) - { - if( retval < 1 ) retval = 1; - close( infd ); - continue; - } - } + if( program_mode == m_compress ) + set_c_outname( input_filename, true, true ); + else set_d_outname( input_filename, eindex ); + if( !open_outstream( force, true ) ) + { close( infd ); set_retval( &retval, 1 ); continue; } } } - Pp_set_name( &pp, input_filename ); - if( !check_tty( pp.name, infd, program_mode ) ) + if( one_to_one && !check_tty_out( program_mode ) ) + { set_retval( &retval, 1 ); return retval; } /* don't delete a tty */ + + if( to_file && outfd < 0 ) /* open outfd after verifying infd */ { - if( retval < 1 ) retval = 1; - if( program_mode == m_test ) { close( infd ); continue; } - cleanup_and_fail( retval ); + if( program_mode == m_compress ) set_c_outname( default_output_filename, + filenames_given, false ); + else + { output_filename = resize_buffer( output_filename, + strlen( default_output_filename ) + 1 ); + strcpy( output_filename, default_output_filename ); } + if( !open_outstream( force, false ) || !check_tty_out( program_mode ) ) + return 1; /* check tty only once and don't try to delete a tty */ } - in_statsp = input_filename[0] ? &in_stats : 0; + in_statsp = ( input_filename[0] && one_to_one ) ? &in_stats : 0; if( program_mode == m_compress ) tmp = compress( &encoder_options, &pp, infd ); else tmp = decompress( 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; + { show_file_error( pp.name, "Error closing input file", errno ); + set_retval( &tmp, 1 ); } + set_retval( &retval, tmp ); if( tmp ) { if( program_mode != m_test ) cleanup_and_fail( retval ); else ++failed_tests; } - if( delete_output_on_interrupt ) + if( delete_output_on_interrupt && one_to_one ) close_and_set_permissions( in_statsp ); - if( input_filename[0] ) - { - if( !keep_input_files && !to_stdout && program_mode != m_test ) - remove( input_filename ); - } + if( input_filename[0] && !keep_input_files && one_to_one ) + remove( input_filename ); } - if( outfd >= 0 && close( outfd ) != 0 ) + if( delete_output_on_interrupt ) close_and_set_permissions( 0 ); /* -o */ + else if( outfd >= 0 && close( outfd ) != 0 ) /* -c */ { show_error( "Error closing stdout", errno, false ); - if( retval < 1 ) retval = 1; + set_retval( &retval, 1 ); } if( failed_tests > 0 && verbosity >= 1 && num_filenames > 1 ) fprintf( stderr, "%s: warning: %d %s failed the test.\n", |