2025-02-24 05:01:08 +01:00
|
|
|
/* Zdiff - decompress and compare two files line by line
|
2025-02-24 05:56:02 +01:00
|
|
|
Copyright (C) 2010-2018 Antonio Diaz Diaz.
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
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
|
2025-02-24 05:47:56 +01:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2025-02-24 05:01:08 +01:00
|
|
|
(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.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
#include <algorithm>
|
2025-02-24 05:01:08 +01:00
|
|
|
#include <cctype>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <climits>
|
|
|
|
#include <csignal>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2025-02-24 05:41:46 +01:00
|
|
|
#if defined(__MSVCRT__) || defined(__OS2__)
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
#include "arg_parser.h"
|
2025-02-24 05:43:00 +01:00
|
|
|
#include "rc.h"
|
2025-02-24 05:45:53 +01:00
|
|
|
#include "zutils.h"
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string fifonames[2]; // names of the two fifos passed to diff
|
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
#include "zcmpdiff.cc"
|
2025-02-24 05:01:08 +01:00
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
void show_help()
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
2025-02-24 05:53:08 +01:00
|
|
|
std::printf( "Zdiff compares two files ('-' means standard input), and if they\n"
|
2025-02-24 05:40:39 +01:00
|
|
|
"differ, shows the differences line by line. If any given file is\n"
|
2025-02-24 05:43:00 +01:00
|
|
|
"compressed, its decompressed content is used. Zdiff is a front end to\n"
|
2025-02-24 05:40:39 +01:00
|
|
|
"the diff program and has the limitation that messages from diff refer to\n"
|
|
|
|
"temporary filenames instead of those specified.\n"
|
|
|
|
"\nThe supported formats are bzip2, gzip, lzip and xz.\n"
|
|
|
|
"\nUsage: zdiff [options] file1 [file2]\n"
|
2025-02-24 05:54:33 +01:00
|
|
|
"\nZdiff compares file1 to file2. If file2 is omitted zdiff tries the\n"
|
2025-02-24 05:40:39 +01:00
|
|
|
"following:\n"
|
2025-02-24 05:54:33 +01:00
|
|
|
"\n 1. If file1 is compressed, compares its decompressed contents with\n"
|
|
|
|
" the corresponding uncompressed file (the name of file1 with the\n"
|
2025-02-24 05:43:00 +01:00
|
|
|
" extension removed).\n"
|
2025-02-24 05:54:33 +01:00
|
|
|
"\n 2. 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"
|
2025-02-24 05:43:00 +01:00
|
|
|
" standard input.\n"
|
2025-02-24 05:40:39 +01:00
|
|
|
"\nExit status is 0 if inputs are identical, 1 if different, 2 if trouble.\n"
|
|
|
|
"\nOptions:\n"
|
2025-02-24 05:51:09 +01:00
|
|
|
" -h, --help display this help and exit\n"
|
|
|
|
" -V, --version output version information and exit\n"
|
|
|
|
" -a, --text treat all files as text\n"
|
|
|
|
" -b, --ignore-space-change ignore changes in the amount of white space\n"
|
|
|
|
" -B, --ignore-blank-lines ignore changes whose lines are all blank\n"
|
|
|
|
" -c use the context output format\n"
|
|
|
|
" -C, --context=<n> same as -c but use <n> lines of context\n"
|
|
|
|
" -d, --minimal try hard to find a smaller set of changes\n"
|
|
|
|
" -E, --ignore-tab-expansion ignore changes due to tab expansion\n"
|
|
|
|
" -i, --ignore-case ignore case differences in file contents\n"
|
2025-02-24 05:52:24 +01:00
|
|
|
" -M, --format=<list> process only the formats in <list>\n"
|
2025-02-24 05:51:09 +01:00
|
|
|
" -N, --no-rcfile don't read runtime configuration file\n"
|
|
|
|
" -O, --force-format=[<f1>][,<f2>] force given formats (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"
|
|
|
|
" -t, --expand-tabs expand tabs to spaces in output\n"
|
|
|
|
" -T, --initial-tab make tabs line up by prepending a tab\n"
|
|
|
|
" -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"
|
|
|
|
" --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"
|
2025-02-24 05:52:24 +01:00
|
|
|
" --xz=<command> set compressor and options for xz format\n" );
|
2025-02-24 05:01:08 +01:00
|
|
|
show_help_addr();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
const char * my_basename( const char * filename )
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
|
|
|
const char * c = filename;
|
|
|
|
while( *c ) { if( *c == '/' ) { filename = c + 1; } ++c; }
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
extern "C" void remove_fifos()
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
|
|
|
if( fifonames[0].size() )
|
|
|
|
{ std::remove( fifonames[0].c_str() ); fifonames[0].clear(); }
|
|
|
|
if( fifonames[1].size() )
|
|
|
|
{ std::remove( fifonames[1].c_str() ); fifonames[1].clear(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:51:09 +01:00
|
|
|
// Set fifonames[i] to "${TMPDIR}/<coded_pid>[_-]<basename(filenames[i])>"
|
2025-02-24 05:01:08 +01:00
|
|
|
// and create FIFOs.
|
|
|
|
bool set_fifonames( const std::string filenames[2] )
|
|
|
|
{
|
|
|
|
enum { num_codes = 36 };
|
|
|
|
const char * const codes = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
const char * p = std::getenv( "TMPDIR" );
|
|
|
|
|
2025-02-24 05:51:09 +01:00
|
|
|
if( p ) { fifonames[0] = p; fifonames[0] += '/'; }
|
|
|
|
else fifonames[0] = "/tmp/";
|
|
|
|
int n = getpid();
|
2025-02-24 05:53:08 +01:00
|
|
|
unsigned pos = fifonames[0].size();
|
2025-02-24 05:51:09 +01:00
|
|
|
do fifonames[0].insert( pos, 1, codes[n % num_codes] );
|
|
|
|
while( n /= num_codes );
|
2025-02-24 05:53:08 +01:00
|
|
|
pos = fifonames[0].size();
|
2025-02-24 05:51:09 +01:00
|
|
|
fifonames[1] = fifonames[0];
|
|
|
|
fifonames[0] += '_'; fifonames[0] += my_basename( filenames[0].c_str() );
|
2025-02-24 05:53:08 +01:00
|
|
|
fifonames[1] += '_'; fifonames[1] += my_basename( filenames[1].c_str() );
|
|
|
|
if( fifonames[1] == fifonames[0] ) fifonames[1][pos] = '-';
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
for( int i = 0; i < 2; ++i )
|
|
|
|
if( mkfifo( fifonames[i].c_str(), S_IRUSR | S_IWUSR ) != 0 )
|
|
|
|
{
|
|
|
|
if( errno == EEXIST )
|
|
|
|
{
|
|
|
|
std::remove( fifonames[i].c_str() );
|
|
|
|
if( mkfifo( fifonames[i].c_str(), S_IRUSR | S_IWUSR ) == 0 )
|
|
|
|
continue;
|
|
|
|
}
|
2025-02-24 05:04:33 +01:00
|
|
|
show_error2( "Can't create FIFO", fifonames[i].c_str() );
|
2025-02-24 05:01:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:54:33 +01:00
|
|
|
bool set_data_feeder( const std::string & filename,
|
|
|
|
const std::string & fifoname, const int infd,
|
2025-02-24 05:43:00 +01:00
|
|
|
Children & children, int format_index )
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
2025-02-24 05:40:39 +01:00
|
|
|
const uint8_t * magic_data = 0;
|
|
|
|
int magic_size = 0;
|
2025-02-24 05:43:00 +01:00
|
|
|
if( format_index < 0 )
|
|
|
|
format_index = test_format( infd, &magic_data, &magic_size );
|
|
|
|
children.compressor_name = get_compressor_name( format_index );
|
2025-02-24 05:01:08 +01:00
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
if( children.compressor_name ) // compressed
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
2025-02-24 05:43:00 +01:00
|
|
|
int fda[2]; // pipe from feeder to compressor
|
2025-02-24 05:01:08 +01:00
|
|
|
if( pipe( fda ) < 0 )
|
|
|
|
{ show_error( "Can't create pipe", errno ); return false; }
|
|
|
|
const pid_t pid = fork();
|
2025-02-24 05:43:00 +01:00
|
|
|
if( pid == 0 ) // child 1 (compressor feeder)
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
|
|
|
if( close( fda[0] ) != 0 ||
|
2025-02-24 05:54:33 +01:00
|
|
|
!feed_data( filename, infd, fda[1], magic_data, magic_size ) )
|
2025-02-24 05:01:08 +01:00
|
|
|
_exit( 2 );
|
|
|
|
if( close( fda[1] ) != 0 )
|
2025-02-24 05:45:53 +01:00
|
|
|
{ show_close_error(); _exit( 2 ); }
|
2025-02-24 05:43:00 +01:00
|
|
|
_exit( 0 );
|
2025-02-24 05:01:08 +01:00
|
|
|
}
|
2025-02-24 05:43:00 +01:00
|
|
|
if( pid < 0 ) // parent
|
|
|
|
{ show_fork_error( "data feeder" ); return false; }
|
|
|
|
|
|
|
|
const pid_t pid2 = fork();
|
|
|
|
if( pid2 == 0 ) // child 2 (compressor)
|
|
|
|
{
|
2025-02-24 05:45:53 +01:00
|
|
|
const int outfd = open( fifoname.c_str(), O_WRONLY | O_BINARY );
|
2025-02-24 05:43:00 +01:00
|
|
|
if( outfd < 0 )
|
|
|
|
{
|
|
|
|
if( verbosity >= 0 )
|
2025-02-24 05:53:08 +01:00
|
|
|
std::fprintf( stderr, "%s: Can't open FIFO '%s' for writing: %s\n",
|
2025-02-24 05:45:53 +01:00
|
|
|
program_name, fifoname.c_str(), std::strerror( errno ) );
|
2025-02-24 05:43:00 +01:00
|
|
|
_exit( 2 );
|
|
|
|
}
|
|
|
|
if( dup2( fda[0], STDIN_FILENO ) >= 0 &&
|
|
|
|
dup2( outfd, STDOUT_FILENO ) >= 0 &&
|
|
|
|
close( fda[0] ) == 0 && close( fda[1] ) == 0 &&
|
|
|
|
close( outfd ) == 0 )
|
|
|
|
{
|
|
|
|
const std::vector< std::string > & compressor_args =
|
|
|
|
get_compressor_args( format_index );
|
|
|
|
const int size = compressor_args.size();
|
|
|
|
const char ** const argv = new const char *[size+3];
|
|
|
|
argv[0] = children.compressor_name;
|
|
|
|
for( int i = 0; i < size; ++i )
|
|
|
|
argv[i+1] = compressor_args[i].c_str();
|
|
|
|
argv[size+1] = ( verbosity >= 0 ) ? "-d" : "-dq";
|
|
|
|
argv[size+2] = 0;
|
|
|
|
execvp( argv[0], (char **)argv );
|
|
|
|
}
|
|
|
|
show_exec_error( children.compressor_name );
|
|
|
|
_exit( 2 );
|
|
|
|
}
|
|
|
|
if( pid2 < 0 ) // parent
|
|
|
|
{ show_fork_error( children.compressor_name ); return false; }
|
|
|
|
|
2025-02-24 05:01:08 +01:00
|
|
|
close( fda[0] ); close( fda[1] );
|
2025-02-24 05:43:00 +01:00
|
|
|
children.pid[0] = pid;
|
|
|
|
children.pid[1] = pid2;
|
2025-02-24 05:01:08 +01:00
|
|
|
}
|
2025-02-24 05:43:00 +01:00
|
|
|
else // uncompressed
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
|
|
|
const pid_t pid = fork();
|
|
|
|
if( pid == 0 ) // child (feeder)
|
|
|
|
{
|
2025-02-24 05:45:53 +01:00
|
|
|
const int outfd = open( fifoname.c_str(), O_WRONLY | O_BINARY );
|
2025-02-24 05:01:08 +01:00
|
|
|
if( outfd < 0 )
|
|
|
|
{
|
|
|
|
if( verbosity >= 0 )
|
2025-02-24 05:53:08 +01:00
|
|
|
std::fprintf( stderr, "%s: Can't open FIFO '%s' for writing: %s\n",
|
2025-02-24 05:45:53 +01:00
|
|
|
program_name, fifoname.c_str(), std::strerror( errno ) );
|
2025-02-24 05:01:08 +01:00
|
|
|
_exit( 2 );
|
|
|
|
}
|
2025-02-24 05:54:33 +01:00
|
|
|
if( !feed_data( filename, infd, outfd, magic_data, magic_size ) )
|
2025-02-24 05:01:08 +01:00
|
|
|
_exit( 2 );
|
|
|
|
if( close( outfd ) != 0 )
|
2025-02-24 05:45:53 +01:00
|
|
|
{ show_close_error(); _exit( 2 ); }
|
2025-02-24 05:01:08 +01:00
|
|
|
_exit( 0 );
|
|
|
|
}
|
2025-02-24 05:43:00 +01:00
|
|
|
if( pid < 0 ) // parent
|
2025-02-24 05:04:33 +01:00
|
|
|
{ show_fork_error( "data feeder" ); return false; }
|
2025-02-24 05:43:00 +01:00
|
|
|
children.pid[0] = pid;
|
|
|
|
children.pid[1] = 0;
|
2025-02-24 05:01:08 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
extern "C" void signal_handler( int sig )
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
|
|
|
remove_fifos();
|
|
|
|
std::signal( sig, SIG_DFL );
|
|
|
|
std::raise( sig );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:40:39 +01:00
|
|
|
void set_signals()
|
2025-02-24 05:01:08 +01:00
|
|
|
{
|
|
|
|
std::signal( SIGHUP, signal_handler );
|
|
|
|
std::signal( SIGINT, signal_handler );
|
|
|
|
std::signal( SIGTERM, signal_handler );
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
|
|
|
|
int main( const int argc, const char * const argv[] )
|
|
|
|
{
|
2025-02-24 05:51:09 +01:00
|
|
|
enum { bz2_opt = 256, gz_opt, lz_opt, xz_opt };
|
2025-02-24 05:01:08 +01:00
|
|
|
std::vector< const char * > diff_args; // args to diff, maybe empty
|
2025-02-24 05:40:39 +01:00
|
|
|
int format_types[2] = { -1, -1 };
|
2025-02-24 05:01:08 +01:00
|
|
|
invocation_name = argv[0];
|
2025-02-24 05:45:53 +01:00
|
|
|
program_name = "zdiff";
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
const Arg_parser::Option options[] =
|
|
|
|
{
|
|
|
|
{ 'a', "text", Arg_parser::no },
|
|
|
|
{ 'b', "ignore-space-change", Arg_parser::no },
|
|
|
|
{ 'B', "ignore-blank-lines", Arg_parser::no },
|
|
|
|
{ 'c', 0, Arg_parser::no },
|
|
|
|
{ 'C', "context", Arg_parser::yes },
|
|
|
|
{ 'd', "minimal", Arg_parser::no },
|
|
|
|
{ 'E', "ignore-tab-expansion", Arg_parser::no },
|
|
|
|
{ 'h', "help", Arg_parser::no },
|
|
|
|
{ 'i', "ignore-case", Arg_parser::no },
|
2025-02-24 05:51:09 +01:00
|
|
|
{ 'M', "format", Arg_parser::yes },
|
2025-02-24 05:43:00 +01:00
|
|
|
{ 'N', "no-rcfile", Arg_parser::no },
|
2025-02-24 05:51:09 +01:00
|
|
|
{ 'O', "force-format", Arg_parser::yes },
|
2025-02-24 05:01:08 +01:00
|
|
|
{ 'p', "show-c-function", Arg_parser::no },
|
|
|
|
{ 'q', "brief", Arg_parser::no },
|
|
|
|
{ 's', "report-identical-files", Arg_parser::no },
|
|
|
|
{ 't', "expand-tabs", Arg_parser::no },
|
|
|
|
{ 'T', "initial-tab", Arg_parser::no },
|
|
|
|
{ 'u', 0, Arg_parser::no },
|
|
|
|
{ 'U', "unified", Arg_parser::yes },
|
|
|
|
{ 'V', "version", Arg_parser::no },
|
|
|
|
{ 'w', "ignore-all-space", Arg_parser::no },
|
2025-02-24 05:43:00 +01:00
|
|
|
{ bz2_opt, "bz2", Arg_parser::yes },
|
|
|
|
{ gz_opt, "gz", Arg_parser::yes },
|
|
|
|
{ lz_opt, "lz", Arg_parser::yes },
|
|
|
|
{ xz_opt, "xz", Arg_parser::yes },
|
2025-02-24 05:01:08 +01:00
|
|
|
{ 0 , 0, Arg_parser::no } };
|
|
|
|
|
|
|
|
const Arg_parser parser( argc, argv, options );
|
|
|
|
if( parser.error().size() ) // bad option
|
|
|
|
{ show_error( parser.error().c_str(), 0, true ); return 2; }
|
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
maybe_process_config_file( parser );
|
|
|
|
|
2025-02-24 05:01:08 +01:00
|
|
|
int argind = 0;
|
|
|
|
for( ; argind < parser.arguments(); ++argind )
|
|
|
|
{
|
|
|
|
const int code = parser.code( argind );
|
|
|
|
if( !code ) break; // no more options
|
2025-02-24 05:51:09 +01:00
|
|
|
const std::string & arg = parser.argument( argind );
|
2025-02-24 05:01:08 +01:00
|
|
|
switch( code )
|
|
|
|
{
|
|
|
|
case 'a': diff_args.push_back( "-a" ); break;
|
|
|
|
case 'b': diff_args.push_back( "-b" ); break;
|
|
|
|
case 'B': diff_args.push_back( "-B" ); break;
|
|
|
|
case 'c': diff_args.push_back( "-c" ); break;
|
2025-02-24 05:51:09 +01:00
|
|
|
case 'C': diff_args.push_back( "-C" );
|
|
|
|
diff_args.push_back( arg.c_str() ); break;
|
2025-02-24 05:01:08 +01:00
|
|
|
case 'd': diff_args.push_back( "-d" ); break;
|
|
|
|
case 'E': diff_args.push_back( "-E" ); break;
|
|
|
|
case 'h': show_help(); return 0;
|
|
|
|
case 'i': diff_args.push_back( "-i" ); break;
|
2025-02-24 05:51:09 +01:00
|
|
|
case 'M': parse_format_list( arg ); break;
|
2025-02-24 05:43:00 +01:00
|
|
|
case 'N': break;
|
2025-02-24 05:51:09 +01:00
|
|
|
case 'O': parse_format_types2( arg, format_types ); break;
|
2025-02-24 05:01:08 +01:00
|
|
|
case 'p': diff_args.push_back( "-p" ); break;
|
|
|
|
case 'q': diff_args.push_back( "-q" ); break;
|
|
|
|
case 's': diff_args.push_back( "-s" ); break;
|
|
|
|
case 't': diff_args.push_back( "-t" ); break;
|
|
|
|
case 'T': diff_args.push_back( "-T" ); break;
|
|
|
|
case 'u': diff_args.push_back( "-u" ); break;
|
2025-02-24 05:51:09 +01:00
|
|
|
case 'U': diff_args.push_back( "-U" );
|
|
|
|
diff_args.push_back( arg.c_str() ); break;
|
2025-02-24 05:47:56 +01:00
|
|
|
case 'V': show_version(); return 0;
|
2025-02-24 05:01:08 +01:00
|
|
|
case 'w': diff_args.push_back( "-w" ); break;
|
2025-02-24 05:43:00 +01:00
|
|
|
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;
|
|
|
|
case xz_opt: parse_compressor( arg, fmt_xz ); break;
|
2025-02-24 05:47:56 +01:00
|
|
|
default : internal_error( "uncaught option." );
|
2025-02-24 05:01:08 +01:00
|
|
|
}
|
|
|
|
} // end process options
|
|
|
|
|
|
|
|
#if defined(__MSVCRT__) || defined(__OS2__)
|
2025-02-24 05:41:46 +01:00
|
|
|
setmode( STDIN_FILENO, O_BINARY );
|
|
|
|
setmode( STDOUT_FILENO, O_BINARY );
|
2025-02-24 05:01:08 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if( argind >= parser.arguments() )
|
|
|
|
{ show_error( "No files given.", 0, true ); return 2; }
|
|
|
|
if( argind + 2 < parser.arguments() )
|
|
|
|
{ show_error( "Too many files.", 0, true ); return 2; }
|
|
|
|
|
|
|
|
const int files = parser.arguments() - argind;
|
|
|
|
std::string filenames[2]; // file names of the two input files
|
|
|
|
filenames[0] = parser.argument( argind );
|
|
|
|
if( files == 2 ) filenames[1] = parser.argument( argind + 1 );
|
|
|
|
|
|
|
|
int infd[2]; // file descriptors of the two files
|
|
|
|
infd[0] = ( filenames[0] == "-" ) ?
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
infd[1] = ( filenames[1] == "-" ) ?
|
|
|
|
STDIN_FILENO : open_instream( filenames[1] );
|
|
|
|
if( infd[1] < 0 ) return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-02-24 05:40:39 +01:00
|
|
|
if( format_types[0] >= 0 || format_types[1] >= 0 )
|
|
|
|
{ show_error( "Two files must be given when format is specified.", 0, true );
|
|
|
|
return 2; }
|
2025-02-24 05:01:08 +01:00
|
|
|
filenames[1] = filenames[0];
|
|
|
|
infd[1] = open_other_instream( filenames[1] );
|
|
|
|
if( infd[1] < 0 ) { infd[1] = STDIN_FILENO; filenames[1] = "-"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
std::atexit( remove_fifos );
|
|
|
|
set_signals();
|
|
|
|
if( !set_fifonames( filenames ) ) return 2;
|
|
|
|
|
2025-02-24 05:45:13 +01:00
|
|
|
Children children[2];
|
2025-02-24 05:54:33 +01:00
|
|
|
if( !set_data_feeder( filenames[0], fifonames[0], infd[0], children[0],
|
|
|
|
format_types[0] ) ||
|
|
|
|
!set_data_feeder( filenames[1], fifonames[1], infd[1], children[1],
|
|
|
|
format_types[1] ) )
|
2025-02-24 05:45:13 +01:00
|
|
|
return 2;
|
|
|
|
|
2025-02-24 05:01:08 +01:00
|
|
|
const pid_t diff_pid = fork();
|
|
|
|
if( diff_pid == 0 ) // child (diff)
|
|
|
|
{
|
|
|
|
const char ** const argv = new const char *[diff_args.size()+5];
|
2025-02-24 05:04:33 +01:00
|
|
|
argv[0] = DIFF;
|
2025-02-24 05:40:39 +01:00
|
|
|
for( unsigned i = 0; i < diff_args.size(); ++i )
|
2025-02-24 05:01:08 +01:00
|
|
|
argv[i+1] = diff_args[i];
|
|
|
|
argv[diff_args.size()+1] = "--";
|
|
|
|
argv[diff_args.size()+2] = fifonames[0].c_str();
|
|
|
|
argv[diff_args.size()+3] = fifonames[1].c_str();
|
|
|
|
argv[diff_args.size()+4] = 0;
|
|
|
|
execvp( argv[0], (char **)argv );
|
2025-02-24 05:04:33 +01:00
|
|
|
show_exec_error( DIFF );
|
2025-02-24 05:01:08 +01:00
|
|
|
_exit( 2 );
|
|
|
|
}
|
2025-02-24 05:43:00 +01:00
|
|
|
if( diff_pid < 0 ) // parent
|
2025-02-24 05:04:33 +01:00
|
|
|
{ show_fork_error( DIFF ); return 2; }
|
2025-02-24 05:01:08 +01:00
|
|
|
|
2025-02-24 05:04:33 +01:00
|
|
|
int retval = wait_for_child( diff_pid, DIFF );
|
2025-02-24 05:01:08 +01:00
|
|
|
|
2025-02-24 05:43:00 +01:00
|
|
|
for( int i = 0; i < 2; ++i )
|
|
|
|
if( !good_status( children[i], retval == 0 ) ) retval = 2;
|
2025-02-24 05:01:08 +01:00
|
|
|
|
|
|
|
for( int i = 0; i < 2; ++i )
|
|
|
|
if( filenames[i] != "-" && close( infd[i] ) != 0 )
|
|
|
|
{
|
2025-02-24 05:04:33 +01:00
|
|
|
show_error2( "Can't close input file", filenames[i].c_str() );
|
2025-02-24 05:01:08 +01:00
|
|
|
retval = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|