summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c386
1 files changed, 217 insertions, 169 deletions
diff --git a/main.c b/main.c
index 8325c6e..327ec69 100644
--- a/main.c
+++ b/main.c
@@ -1,24 +1,24 @@
-/* Clzip - LZMA lossless data compressor
- Copyright (C) 2010-2019 Antonio Diaz Diaz.
+/* Clzip - LZMA lossless data compressor
+ Copyright (C) 2010-2021 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
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
+ 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
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
- 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. See the
- GNU General Public License for more details.
+ 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. See the
+ GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
- 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 clzip 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 clzip to panic.
*/
#define _FILE_OFFSET_BITS 64
@@ -74,11 +74,11 @@
int verbosity = 0;
-const char * const program_name = "clzip";
-const char * const program_year = "2019";
-const char * invocation_name = 0;
+static const char * const program_name = "clzip";
+static const char * const program_year = "2021";
+static const char * invocation_name = "clzip"; /* default value */
-const struct { const char * from; const char * to; } known_extensions[] = {
+static const struct { const char * from; const char * to; } known_extensions[] = {
{ ".lz", "" },
{ ".tlz", ".tar" },
{ 0, 0 } };
@@ -93,24 +93,26 @@ 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;
+static char * output_filename = 0;
+static int outfd = -1;
+static bool delete_output_on_interrupt = false;
static void show_help( void )
{
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"
+ "applications like package managers, embedded devices, or systems lacking a\n"
+ "C++ compiler.\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"
"\nUsage: %s [options] [files]\n", invocation_name );
printf( "\nOptions:\n"
" -h, --help display this help and exit\n"
@@ -124,7 +126,7 @@ static void show_help( void )
" -k, --keep keep (don't delete) input files\n"
" -l, --list print (un)compressed file sizes\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"
" -S, --volume-size=<bytes> set volume size limit in bytes\n"
@@ -134,7 +136,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 '-', clzip compresses or\n"
+ "\nIf no file names are given, or if a file is '-', clzip 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"
@@ -142,12 +144,19 @@ static void show_help( void )
"to 2^29 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.\n"
+ "etc, you may need to use the options --dictionary-size and --match-length\n"
+ "directly to achieve optimal performance.\n"
+ "\nTo extract all the files from archive 'foo.tar.lz', use the commands\n"
+ "'tar -xf foo.tar.lz' or 'clzip -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 clzip to panic.\n"
+ "\nThe ideas embodied in clzip are due to (at least) the following people:\n"
+ "Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for the\n"
+ "definition of Markov chains), G.N.N. Martin (for the definition of range\n"
+ "encoding), Igor Pavlov (for putting all the above together in LZMA), and\n"
+ "Julian Seward (for bzip2's CLI).\n"
"\nReport bugs to lzip-bug@nongnu.org\n"
"Clzip home page: http://www.nongnu.org/lzip/clzip.html\n" );
}
@@ -168,14 +177,63 @@ 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;
+ }
+
+static void Pp_reset( struct Pretty_print * const pp )
+ { if( pp->name && pp->name[0] ) pp->first_post = true; }
void Pp_show_msg( struct Pretty_print * const pp, const char * const msg )
{
@@ -222,7 +280,7 @@ const char * format_ds( const unsigned dictionary_size )
void show_header( const unsigned dictionary_size )
{
- fprintf( stderr, "dictionary %s, ", format_ds( dictionary_size ) );
+ fprintf( stderr, "dict %s, ", format_ds( dictionary_size ) );
}
@@ -289,7 +347,7 @@ static int get_dict_size( const char * const arg )
}
-void set_mode( enum Mode * const program_modep, const enum Mode new_mode )
+static void set_mode( enum Mode * const program_modep, const enum Mode new_mode )
{
if( *program_modep != m_compress && *program_modep != new_mode )
{
@@ -316,14 +374,17 @@ static int extension_index( const char * const name )
}
-static void set_c_outname( const char * const name, const bool force_ext,
- const bool multifile )
+static void set_c_outname( const char * const name, const bool filenames_given,
+ const bool force_ext, const bool multifile )
{
+ /* 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 ) + 5 +
strlen( known_extensions[0].from ) + 1 );
strcpy( output_filename, name );
if( multifile ) strcat( output_filename, "00001" );
- if( force_ext || multifile || extension_index( output_filename ) < 0 )
+ if( force_ext || multifile ||
+ ( !filenames_given && extension_index( output_filename ) < 0 ) )
strcat( output_filename, known_extensions[0].from );
}
@@ -354,7 +415,7 @@ static void set_d_outname( const char * const name, const int eindex )
int open_instream( const char * const name, struct stat * const in_statsp,
- const bool no_ofile, const bool reg_only )
+ const bool one_to_one, const bool reg_only )
{
int infd = open( name, O_RDONLY | O_BINARY );
if( infd < 0 )
@@ -366,13 +427,12 @@ int open_instream( const char * const name, struct stat * const in_statsp,
const bool can_read = ( i == 0 && !reg_only &&
( S_ISBLK( mode ) || S_ISCHR( mode ) ||
S_ISFIFO( mode ) || S_ISSOCK( mode ) ) );
- 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;
}
@@ -383,9 +443,8 @@ int open_instream( const char * const name, struct stat * const in_statsp,
static int open_instream2( 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 )
{
- const bool no_ofile = ( to_stdout || program_mode == m_test );
if( program_mode == m_compress && !recompress && eindex >= 0 )
{
if( verbosity >= 0 )
@@ -393,15 +452,15 @@ static int open_instream2( const char * const name, struct stat * const in_stats
program_name, name, known_extensions[eindex].from );
return -1;
}
- return open_instream( name, in_statsp, no_ofile, false );
+ return open_instream( name, in_statsp, one_to_one, false );
}
-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;
@@ -420,25 +479,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 );
@@ -464,7 +504,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 );
@@ -472,7 +512,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;
@@ -591,7 +655,7 @@ static int compress( const unsigned long long cfile_size,
close_and_set_permissions( in_statsp );
if( !next_filename() )
{ Pp_show_msg( pp, "Too many volume files." ); retval = 1; break; }
- if( !open_outstream( true, !in_statsp ) ) { retval = 1; break; }
+ if( !open_outstream( true, in_statsp ) ) { retval = 1; break; }
}
}
}
@@ -661,10 +725,7 @@ static int decompress( const unsigned long long cfile_size, const int infd,
int retval = 0;
bool first_member;
if( !Rd_init( &rdec, infd ) )
- {
- show_error( "Not enough memory.", 0, false );
- cleanup_and_fail( 1 );
- }
+ { show_error( mem_msg, 0, false ); cleanup_and_fail( 1 ); }
for( first_member = true; ; first_member = false )
{
@@ -711,7 +772,7 @@ static int decompress( const unsigned long long cfile_size, const int infd,
Pp_show_msg( pp, 0 );
if( !LZd_init( &decoder, &rdec, dictionary_size, outfd ) )
- { Pp_show_msg( pp, "Not enough memory." ); retval = 1; break; }
+ { Pp_show_msg( pp, mem_msg ); retval = 1; break; }
show_dprogress( cfile_size, partial_file_pos, &rdec, pp ); /* init */
result = LZd_decode_member( &decoder, pp );
partial_file_pos += Rd_member_position( &rdec );
@@ -845,12 +906,14 @@ int main( const int argc, const char * const argv[] )
{ 3 << 23, 132 }, /* -8 */
{ 1 << 25, 273 } }; /* -9 */
struct Lzma_options encoder_options = option_mapping[6]; /* default = "-6" */
- const unsigned long long max_member_size = 0x0008000000000000ULL;
- const unsigned long long max_volume_size = 0x4000000000000000ULL;
+ const unsigned long long max_member_size = 0x0008000000000000ULL; /* 2 PiB */
+ const unsigned long long max_volume_size = 0x4000000000000000ULL; /* 4 EiB */
unsigned long long member_size = max_member_size;
unsigned long long volume_size = 0;
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;
@@ -866,20 +929,19 @@ int main( const int argc, const char * const argv[] )
bool stdin_used = false;
bool to_stdout = false;
bool zero = 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 },
@@ -900,15 +962,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 } };
-
- struct Arg_parser parser;
+ { 0, 0, ap_no } };
- 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; }
@@ -936,7 +996,8 @@ int main( const int argc, const char * const argv[] )
getnum( arg, min_match_len_limit, max_match_len );
zero = false; 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 );
zero = false; break;
@@ -967,21 +1028,33 @@ int main( const int argc, const char * const argv[] )
if( program_mode == m_list )
return list_files( filenames, num_filenames, ignore_trailing, loose_trailing );
- if( program_mode == m_test )
- outfd = -1;
- else if( program_mode == m_compress )
+ if( program_mode == m_compress )
{
+ if( volume_size > 0 && !to_stdout && default_output_filename[0] &&
+ num_filenames > 1 )
+ { show_error( "Only can compress one file when using '-o' and '-S'.",
+ 0, true ); return 1; }
Dis_slots_init();
Prob_prices_init();
}
+ else volume_size = 0;
+ 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 )
{
unsigned long long cfile_size;
@@ -990,70 +1063,50 @@ 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, volume_size > 0 );
- 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_instream2( 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, volume_size > 0 );
- 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, volume_size > 0 );
+ 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, volume_size > 0 );
+ 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;
- cfile_size = ( in_statsp && S_ISREG( in_statsp->st_mode ) ) ?
- ( in_statsp->st_size + 99 ) / 100 : 0;
+ in_statsp = ( input_filename[0] && one_to_one ) ? &in_stats : 0;
+ cfile_size = ( input_filename[0] && S_ISREG( in_stats.st_mode ) ) ?
+ ( in_stats.st_size + 99 ) / 100 : 0;
if( program_mode == m_compress )
tmp = compress( cfile_size, member_size, volume_size, infd,
&encoder_options, &pp, in_statsp, zero );
@@ -1061,29 +1114,24 @@ int main( const int argc, const char * const argv[] )
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;
+ { 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 &&
- ( program_mode != m_compress || volume_size == 0 ) )
- remove( input_filename );
- }
+ if( input_filename[0] && !keep_input_files && one_to_one &&
+ ( program_mode != m_compress || volume_size == 0 ) )
+ 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",