summaryrefslogtreecommitdiffstats
path: root/zdiff.cc
diff options
context:
space:
mode:
Diffstat (limited to 'zdiff.cc')
-rw-r--r--zdiff.cc83
1 files changed, 48 insertions, 35 deletions
diff --git a/zdiff.cc b/zdiff.cc
index b9106c7..9312253 100644
--- a/zdiff.cc
+++ b/zdiff.cc
@@ -1,18 +1,18 @@
-/* Zdiff - decompress and compare two files line by line
- Copyright (C) 2010-2019 Antonio Diaz Diaz.
+/* Zdiff - decompress and compare two files line by line
+ 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
@@ -39,6 +39,7 @@
#include "rc.h"
#include "zutils.h"
+// 'verbosity' is always 0 in zdiff; no --verbose or --quiet available.
namespace {
@@ -48,23 +49,23 @@ std::string fifonames[2]; // names of the two fifos passed to diff
void show_help()
{
- std::printf( "Zdiff compares two files ('-' means standard input), and if they\n"
- "differ, shows the differences line by line. If any given file is\n"
- "compressed, its decompressed content is used. Zdiff is a front end to\n"
- "the diff program and has the limitation that messages from diff refer to\n"
- "temporary filenames instead of those specified.\n"
- "\nThe formats supported are bzip2, gzip, lzip and xz.\n"
+ std::printf( "zdiff compares two files and, if they differ, writes to standard output the\n"
+ "differences line by line. A hyphen '-' used as a file argument means standard\n"
+ "input. If any file given is compressed, its decompressed content is used.\n"
+ "zdiff is a front end to the program diff and has the limitation that messages\n"
+ "from diff refer to temporary file names instead of those specified.\n"
+ "\nThe formats supported are bzip2, gzip, lzip, and xz.\n"
"\nUsage: zdiff [options] file1 [file2]\n"
- "\nZdiff compares file1 to file2. If file2 is omitted zdiff tries the\n"
+ "\nzdiff compares file1 to file2. The standard input is used only if file1 or\n"
+ "file2 refers to standard input. If file2 is omitted zdiff 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"
+ "Some options only work if the diff program used supports them.\n"
"\nOptions:\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n"
@@ -78,7 +79,7 @@ void show_help()
" -i, --ignore-case ignore case differences in file contents\n"
" -M, --format=<list> process only the formats in <list>\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"
" -p, --show-c-function show which C function each change is in\n"
" -q, --brief output only whether files differ\n"
" -s, --report-identical-files report when two files are identical\n"
@@ -87,6 +88,8 @@ void show_help()
" -u use the unified output format\n"
" -U, --unified=<n> same as -u but use <n> lines of context\n"
" -w, --ignore-all-space ignore all white space\n"
+ " -W, --width=<n> output at most <n> print columns\n"
+ " -y, --side-by-side output in two columns\n"
" --bz2=<command> set compressor and options for bzip2 format\n"
" --gz=<command> set compressor and options for gzip format\n"
" --lz=<command> set compressor and options for lzip format\n"
@@ -112,8 +115,8 @@ extern "C" void remove_fifos()
}
-// Set fifonames[i] to "${TMPDIR}/<coded_pid>[_-]<basename(filenames[i])>"
-// and create FIFOs.
+/* Set fifonames[i] to "${TMPDIR}/<coded_pid>[_-]<basename(filenames[i])>"
+ and create FIFOs. */
bool set_fifonames( const std::string filenames[2] )
{
enum { num_codes = 36 };
@@ -152,10 +155,10 @@ bool set_data_feeder( const std::string & filename,
const std::string & fifoname, const int infd,
Children & children, int format_index )
{
- const uint8_t * magic_data = 0;
+ uint8_t magic_data[magic_buf_size];
int magic_size = 0;
if( format_index < 0 )
- format_index = test_format( infd, &magic_data, &magic_size );
+ format_index = test_format( infd, magic_data, &magic_size );
children.compressor_name = get_compressor_name( format_index );
if( children.compressor_name ) // compressed
@@ -264,8 +267,8 @@ int main( const int argc, const char * const argv[] )
enum { bz2_opt = 256, gz_opt, lz_opt, xz_opt };
std::vector< const char * > diff_args; // args to diff, maybe empty
int format_types[2] = { -1, -1 };
- invocation_name = argv[0];
program_name = "zdiff";
+ invocation_name = ( argc > 0 ) ? argv[0] : program_name;
const Arg_parser::Option options[] =
{
@@ -290,6 +293,8 @@ int main( const int argc, const char * const argv[] )
{ 'U', "unified", Arg_parser::yes },
{ 'V', "version", Arg_parser::no },
{ 'w', "ignore-all-space", Arg_parser::no },
+ { 'W', "width", Arg_parser::yes },
+ { 'y', "side-by-side", Arg_parser::no },
{ bz2_opt, "bz2", Arg_parser::yes },
{ gz_opt, "gz", Arg_parser::yes },
{ lz_opt, "lz", Arg_parser::yes },
@@ -333,6 +338,9 @@ int main( const int argc, const char * const argv[] )
diff_args.push_back( arg.c_str() ); break;
case 'V': show_version(); return 0;
case 'w': diff_args.push_back( "-w" ); break;
+ case 'W': diff_args.push_back( "-W" );
+ diff_args.push_back( arg.c_str() ); break;
+ case 'y': diff_args.push_back( "-y" ); break;
case bz2_opt: parse_compressor( arg, fmt_bz2 ); break;
case gz_opt: parse_compressor( arg, fmt_gz ); break;
case lz_opt: parse_compressor( arg, fmt_lz ); break;
@@ -361,25 +369,30 @@ 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() ) ) )
- return 0;
-
if( files == 2 )
{
+ if( check_identical( filenames[0].c_str(), filenames[1].c_str() ) )
+ return 0;
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;
+ }
}
std::atexit( remove_fifos );