summaryrefslogtreecommitdiffstats
path: root/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'main.cc')
-rw-r--r--main.cc90
1 files changed, 44 insertions, 46 deletions
diff --git a/main.cc b/main.cc
index c23b3bc..2de410b 100644
--- a/main.cc
+++ b/main.cc
@@ -102,7 +102,9 @@ enum Mode { m_compress, m_decompress, m_test };
std::string output_filename;
int outfd = -1;
int verbosity = 0;
-mode_t outfd_mode = S_IRUSR | S_IWUSR;
+const mode_t usr_rw = S_IRUSR | S_IWUSR;
+const mode_t all_rw = usr_rw | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+mode_t outfd_mode = usr_rw;
bool delete_output_on_interrupt = false;
@@ -110,31 +112,31 @@ void show_help() throw()
{
std::printf( "%s - Data compressor based on the LZMA algorithm.\n", Program_name );
std::printf( "\nUsage: %s [options] [files]\n", invocation_name );
- std::printf( "\nOptions:\n" );
- std::printf( " -h, --help display this help and exit\n" );
- std::printf( " -V, --version output version information and exit\n" );
- std::printf( " -b, --member-size=<n> set member size limit in bytes\n" );
- std::printf( " -c, --stdout send output to standard output\n" );
- std::printf( " -d, --decompress decompress\n" );
- std::printf( " -f, --force overwrite existing output files\n" );
- std::printf( " -F, --recompress force recompression of compressed files\n" );
- std::printf( " -k, --keep keep (don't delete) input files\n" );
- std::printf( " -m, --match-length=<n> set match length limit in bytes [36]\n" );
- std::printf( " -o, --output=<file> if reading stdin, place the output into <file>\n" );
- std::printf( " -q, --quiet suppress all messages\n" );
- std::printf( " -s, --dictionary-size=<n> set dictionary size limit in bytes [8MiB]\n" );
- std::printf( " -S, --volume-size=<n> set volume size limit in bytes\n" );
- std::printf( " -t, --test test compressed file integrity\n" );
- std::printf( " -v, --verbose be verbose (a 2nd -v gives more)\n" );
- std::printf( " -0 .. -9 set compression level [default 6]\n" );
- std::printf( " --fast alias for -0\n" );
- std::printf( " --best alias for -9\n" );
- std::printf( "If no file names are given, %s compresses or decompresses\n", program_name );
- std::printf( "from standard input to standard output.\n" );
- std::printf( "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n" );
- std::printf( "Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" );
- std::printf( "\nReport bugs to lzip-bug@nongnu.org\n" );
- std::printf( "Lzip home page: http://www.nongnu.org/lzip/lzip.html\n" );
+ std::printf( "\nOptions:\n"
+ " -h, --help display this help and exit\n"
+ " -V, --version output version information and exit\n"
+ " -b, --member-size=<bytes> set member size limit in bytes\n"
+ " -c, --stdout send output to standard output\n"
+ " -d, --decompress decompress\n"
+ " -f, --force overwrite existing output files\n"
+ " -F, --recompress force recompression 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 stdin, place the output into <file>\n"
+ " -q, --quiet suppress all messages\n"
+ " -s, --dictionary-size=<bytes> set dictionary size limit in bytes [8MiB]\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"
+ " --fast alias for -0\n"
+ " --best alias for -9\n"
+ "If no file names are given, lzip compresses or decompresses\n"
+ "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"
+ "\nReport bugs to lzip-bug@nongnu.org\n"
+ "Lzip home page: http://www.nongnu.org/lzip/lzip.html\n" );
}
@@ -142,9 +144,9 @@ void show_version() throw()
{
std::printf( "%s %s\n", Program_name, PROGVERSION );
std::printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year );
- std::printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" );
- std::printf( "This is free software: you are free to change and redistribute it.\n" );
- std::printf( "There is NO WARRANTY, to the extent permitted by law.\n" );
+ std::printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
+ "This is free software: you are free to change and redistribute it.\n"
+ "There is NO WARRANTY, to the extent permitted by law.\n" );
}
@@ -155,10 +157,11 @@ const char * format_num( long long num ) throw()
enum { buf_size = 16, factor = 1024 };
static char buf[buf_size];
const char *p = "";
+ bool exact = ( num % factor == 0 );
for( int i = 0; i < 8 && ( llabs( num ) > 9999 ||
- ( llabs( num ) >= factor && num % factor == 0 ) ); ++i )
- { num /= factor; p = prefix[i]; }
+ ( 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;
}
@@ -369,31 +372,26 @@ void cleanup_and_fail( const int retval ) throw()
// Set permissions, owner and times.
void close_and_set_permissions( const struct stat * const in_statsp )
{
- bool error = false;
+ bool warning = false;
if( in_statsp )
{
+ // fchown will in many cases return with EPERM, which can be safely ignored.
if( ( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) != 0 &&
errno != EPERM ) ||
- fchmod( outfd, in_statsp->st_mode ) != 0 )
- error = true;
- // fchown will in many cases return with EPERM, which can be safely ignored.
+ fchmod( outfd, in_statsp->st_mode ) != 0 ) warning = true;
}
- if( close( outfd ) == 0 ) outfd = -1;
- else cleanup_and_fail( 1 );
+ if( close( outfd ) != 0 ) cleanup_and_fail( 1 );
+ outfd = -1;
delete_output_on_interrupt = false;
- if( !in_statsp ) return;
- if( !error )
+ if( in_statsp )
{
struct utimbuf t;
t.actime = in_statsp->st_atime;
t.modtime = in_statsp->st_mtime;
- if( utime( output_filename.c_str(), &t ) != 0 ) error = true;
+ if( utime( output_filename.c_str(), &t ) != 0 ) warning = true;
}
- if( error )
- {
+ if( warning && verbosity >= 1 )
show_error( "Can't change output file attributes." );
- cleanup_and_fail( 1 );
- }
}
@@ -852,7 +850,7 @@ int main( const int argc, const char * const argv[] )
if( program_mode == m_compress )
set_c_outname( default_output_filename, volume_size != LLONG_MAX );
else output_filename = default_output_filename;
- outfd_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+ outfd_mode = all_rw;
if( !open_outstream( force ) )
{
if( outfd == -1 && retval < 1 ) retval = 1;
@@ -877,7 +875,7 @@ int main( const int argc, const char * const argv[] )
if( program_mode == m_compress )
set_c_outname( input_filename, volume_size != LLONG_MAX );
else set_d_outname( input_filename, eindex );
- outfd_mode = S_IRUSR | S_IWUSR;
+ outfd_mode = usr_rw;
if( !open_outstream( force ) )
{
if( outfd == -1 && retval < 1 ) retval = 1;