summaryrefslogtreecommitdiffstats
path: root/zcmp.cc
diff options
context:
space:
mode:
Diffstat (limited to 'zcmp.cc')
-rw-r--r--zcmp.cc74
1 files changed, 39 insertions, 35 deletions
diff --git a/zcmp.cc b/zcmp.cc
index 2408ca0..b97d5ef 100644
--- a/zcmp.cc
+++ b/zcmp.cc
@@ -1,18 +1,18 @@
-/* Zcmp - decompress and compare two files byte by byte
- Copyright (C) 2010-2019 Antonio Diaz Diaz.
+/* Zcmp - decompress and compare two files byte by byte
+ Copyright (C) 2010-2020 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/>.
*/
#define _FILE_OFFSET_BITS 64
@@ -50,22 +50,21 @@ namespace {
void show_help()
{
- std::printf( "Zcmp compares two files ('-' means standard input), and if they differ,\n"
- "tells the first byte and line number where they differ. Bytes and lines\n"
- "are numbered starting with 1. If any given file is compressed, its\n"
- "decompressed content is used. Compressed files are decompressed on the\n"
- "fly; no temporary files are created.\n"
- "\nThe formats supported are bzip2, gzip, lzip and xz.\n"
+ std::printf( "zcmp compares two files and, if they differ, writes to standard output the\n"
+ "first byte and line number where they differ. Bytes and lines are numbered\n"
+ "starting with 1. A hyphen '-' used as a file argument means standard input.\n"
+ "If any file given is compressed, its decompressed content is used. Compressed\n"
+ "files are decompressed on the fly; no temporary files are created.\n"
+ "\nThe formats supported are bzip2, gzip, lzip, and xz.\n"
"\nUsage: zcmp [options] file1 [file2]\n"
- "\nZcmp compares file1 to file2. If file2 is omitted zcmp tries the\n"
+ "\nzcmp compares file1 to file2. The standard input is used only if file1 or\n"
+ "file2 refers to standard input. If file2 is omitted zcmp tries the\n"
"following:\n"
- "\n 1. If file1 is compressed, compares its decompressed contents with\n"
+ "\n - If file1 is compressed, compares its decompressed contents with\n"
" the corresponding uncompressed file (the name of file1 with the\n"
" extension removed).\n"
- "\n 2. If file1 is uncompressed, compares it with the decompressed\n"
+ "\n - If file1 is uncompressed, compares it with the decompressed\n"
" contents of file1.[lz|bz2|gz|xz] (the first one that is found).\n"
- "\n 3. If no suitable file is found, compares file1 with data read from\n"
- " standard input.\n"
"\nExit status is 0 if inputs are identical, 1 if different, 2 if trouble.\n"
"\nOptions:\n"
" -h, --help display this help and exit\n"
@@ -76,7 +75,7 @@ void show_help()
" -M, --format=<list> process only the formats in <list>\n"
" -n, --bytes=<n> compare at most <n> bytes\n"
" -N, --no-rcfile don't read runtime configuration file\n"
- " -O, --force-format=[<f1>][,<f2>] force given formats (bz2, gz, lz, xz)\n"
+ " -O, --force-format=[<f1>][,<f2>] force the formats given (bz2, gz, lz, xz)\n"
" -q, --quiet suppress all messages\n"
" -s, --silent (same as --quiet)\n"
" -v, --verbose verbose mode (same as --list)\n"
@@ -84,7 +83,7 @@ void show_help()
" --gz=<command> set compressor and options for gzip format\n"
" --lz=<command> set compressor and options for lzip format\n"
" --xz=<command> set compressor and options for xz format\n"
- "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n"
+ "\nNumbers 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" );
show_help_addr();
}
@@ -326,8 +325,8 @@ int main( const int argc, const char * const argv[] )
long long max_size = -1; // < 0 means unlimited size
int format_types[2] = { -1, -1 };
bool print_bytes = false;
- invocation_name = argv[0];
program_name = "zcmp";
+ invocation_name = ( argc > 0 ) ? argv[0] : program_name;
const Arg_parser::Option options[] =
{
@@ -403,28 +402,33 @@ int main( const int argc, const char * const argv[] )
STDIN_FILENO : open_instream( filenames[0] );
if( infd[0] < 0 ) return 2;
- if( ( files == 1 && filenames[0] == "-" ) ||
- ( files == 2 && check_identical( filenames[0].c_str(),
- filenames[1].c_str() ) ) )
- {
- if( ignore_initial[0] == ignore_initial[1] ) return 0;
- else { show_error( "Can't compare parts of same file." ); return 2; }
- }
-
if( files == 2 )
{
+ if( check_identical( filenames[0].c_str(), filenames[1].c_str() ) )
+ {
+ if( ignore_initial[0] == ignore_initial[1] ) return 0;
+ else { show_error( "Can't compare parts of same file." ); return 2; }
+ }
infd[1] = ( filenames[1] == "-" ) ?
STDIN_FILENO : open_instream( filenames[1] );
if( infd[1] < 0 ) return 2;
}
else
{
+ if( filenames[0] == "-" )
+ { show_error( "Missing operand after '-'.", 0, true ); return 2; }
if( format_types[0] >= 0 || format_types[1] >= 0 )
{ show_error( "Two files must be given when format is specified.", 0, true );
return 2; }
filenames[1] = filenames[0];
infd[1] = open_other_instream( filenames[1] );
- if( infd[1] < 0 ) { infd[1] = STDIN_FILENO; filenames[1] = "-"; }
+ if( infd[1] < 0 )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Can't find file to compare with '%s'.\n",
+ program_name, filenames[0].c_str() );
+ show_error( 0, 0, true ); return 2;
+ }
}
int old_infd[2]; // copy of file descriptors of the two files