| 1 |
/* GNU cmp - compare two files byte by byte |
| 2 |
|
| 3 |
Copyright (C) 1990-1996, 1998, 2001-2002, 2004, 2006-2007, 2009-2013, |
| 4 |
2015-2025 Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This program is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#include "system.h" |
| 20 |
#include "paths.h" |
| 21 |
|
| 22 |
#include <binary-io.h> |
| 23 |
#include <c-ctype.h> |
| 24 |
#include <c-stack.h> |
| 25 |
#include <cmpbuf.h> |
| 26 |
#include <diagnose.h> |
| 27 |
#include <error.h> |
| 28 |
#include <exitfail.h> |
| 29 |
#include <file-type.h> |
| 30 |
#include <getopt.h> |
| 31 |
#include <hard-locale.h> |
| 32 |
#include <progname.h> |
| 33 |
#include <quote.h> |
| 34 |
#include <unlocked-io.h> |
| 35 |
#include <version-etc.h> |
| 36 |
#include <xalloc.h> |
| 37 |
#include <xstdopen.h> |
| 38 |
#include <xstrtol.h> |
| 39 |
|
| 40 |
#include <stdio.h> |
| 41 |
|
| 42 |
/* The official name of this program (e.g., no 'g' prefix). */ |
| 43 |
static char const PROGRAM_NAME[] = "cmp"; |
| 44 |
|
| 45 |
#define AUTHORS \ |
| 46 |
proper_name_lite ("Torbjorn Granlund", "Torbj\303\266rn Granlund"), \ |
| 47 |
_("David MacKenzie") |
| 48 |
|
| 49 |
static bool |
| 50 |
hard_locale_LC_MESSAGES (void) |
| 51 |
{ |
| 52 |
#if defined LC_MESSAGES && ENABLE_NLS |
| 53 |
return hard_locale (LC_MESSAGES); |
| 54 |
#else |
| 55 |
return false; |
| 56 |
#endif |
| 57 |
} |
| 58 |
|
| 59 |
static int cmp (void); |
| 60 |
static off_t file_position (int); |
| 61 |
static idx_t block_compare (word const *, word const *) ATTRIBUTE_PURE; |
| 62 |
static idx_t count_newlines (char *, idx_t); |
| 63 |
static void sprintc (char *, unsigned char); |
| 64 |
|
| 65 |
/* Filenames of the compared files. */ |
| 66 |
static char const *file[2]; |
| 67 |
|
| 68 |
/* File descriptors of the files. */ |
| 69 |
static int file_desc[2]; |
| 70 |
|
| 71 |
/* Status of the files. If st_size is -1, stat_buf[i] is valid except |
| 72 |
that the file size is unspecified. If st_size is -2, the rest of |
| 73 |
stat_buf[i] is unspecified except that st_blksize (if it exists) is |
| 74 |
a reasonable guess. */ |
| 75 |
static struct stat stat_buf[2]; |
| 76 |
|
| 77 |
/* Read buffers for the files. */ |
| 78 |
static word *buffer[2]; |
| 79 |
|
| 80 |
/* Optimal block size for the files. */ |
| 81 |
static idx_t buf_size; |
| 82 |
|
| 83 |
/* Initial prefix to ignore for each file, or negative if the user |
| 84 |
requested to ignore more than TYPE_MAXIMUM (intmax_t) bytes. */ |
| 85 |
static intmax_t ignore_initial[2]; |
| 86 |
|
| 87 |
/* Number of bytes to compare. INTMAX_MAX is effectively infinity, |
| 88 |
since there's no practical way on current computers to compare so |
| 89 |
many bytes. Even if cmp added SEEK_HOLE and SEEK_DATA optimization, |
| 90 |
regular files can't have more than TYPE_MAXIMUM (off_t) bytes |
| 91 |
and special files are unlikely to support this optimization. */ |
| 92 |
static intmax_t bytes = INTMAX_MAX; |
| 93 |
|
| 94 |
/* Output format. */ |
| 95 |
static enum comparison_type |
| 96 |
{ |
| 97 |
type_first_diff, /* Print the first difference. */ |
| 98 |
type_all_diffs, /* Print all differences. */ |
| 99 |
type_no_stdout, /* Do not output to stdout; only stderr. */ |
| 100 |
type_status /* Exit status only. */ |
| 101 |
} comparison_type; |
| 102 |
|
| 103 |
/* If nonzero, print values of bytes quoted like cat -t does. */ |
| 104 |
static bool opt_print_bytes; |
| 105 |
|
| 106 |
/* Values for long options that do not have single-letter equivalents. */ |
| 107 |
enum |
| 108 |
{ |
| 109 |
HELP_OPTION = CHAR_MAX + 1 |
| 110 |
}; |
| 111 |
|
| 112 |
static char const shortopts[] = "bci:ln:sv"; |
| 113 |
static struct option const longopts[] = |
| 114 |
{ |
| 115 |
{"print-bytes", 0, 0, 'b'}, |
| 116 |
{"print-chars", 0, 0, 'c'}, /* obsolescent as of diffutils 2.7.3 */ |
| 117 |
{"ignore-initial", 1, 0, 'i'}, |
| 118 |
{"verbose", 0, 0, 'l'}, |
| 119 |
{"bytes", 1, 0, 'n'}, |
| 120 |
{"silent", 0, 0, 's'}, |
| 121 |
{"quiet", 0, 0, 's'}, |
| 122 |
{"version", 0, 0, 'v'}, |
| 123 |
{"help", 0, 0, HELP_OPTION}, |
| 124 |
{0, 0, 0, 0} |
| 125 |
}; |
| 126 |
|
| 127 |
static char const valid_suffixes[] = "kKMGTPEZY0"; |
| 128 |
|
| 129 |
/* Update ignore_initial[F] according to the result of parsing an |
| 130 |
*operand ARGPTR of --ignore-initial, updating *ARGPTR to point |
| 131 |
*after the operand. If DELIMITER is nonzero, the operand may be |
| 132 |
*followed by DELIMITER; otherwise it must be null-terminated. */ |
| 133 |
static void |
| 134 |
specify_ignore_initial (int f, char **argptr, char delimiter) |
| 135 |
{ |
| 136 |
intmax_t val; |
| 137 |
char const *arg = *argptr; |
| 138 |
strtol_error d = xstrtoimax (arg, argptr, 0, &val, valid_suffixes); |
| 139 |
strtol_error e = d & ~LONGINT_OVERFLOW; |
| 140 |
if (! ((e == LONGINT_OK |
| 141 |
|| (e == LONGINT_INVALID_SUFFIX_CHAR && **argptr == delimiter)) |
| 142 |
&& 0 <= val)) |
| 143 |
try_help ("invalid --ignore-initial value %s", quote (arg)); |
| 144 |
if (0 <= ignore_initial[f] && ignore_initial[f] < val) |
| 145 |
ignore_initial[f] = d == e ? val : -1; |
| 146 |
} |
| 147 |
|
| 148 |
/* Specify the output format. */ |
| 149 |
static void |
| 150 |
specify_comparison_type (enum comparison_type t) |
| 151 |
{ |
| 152 |
if (comparison_type && comparison_type != t) |
| 153 |
try_help ("options -l and -s are incompatible", nullptr); |
| 154 |
comparison_type = t; |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
check_stdout (void) |
| 159 |
{ |
| 160 |
if (ferror (stdout)) |
| 161 |
error (EXIT_TROUBLE, 0, "%s", _("write failed")); |
| 162 |
else if (fclose (stdout) != 0) |
| 163 |
error (EXIT_TROUBLE, errno, "%s", _("standard output")); |
| 164 |
} |
| 165 |
|
| 166 |
static char const *const option_help_msgid[] = { |
| 167 |
N_("-b, --print-bytes print differing bytes"), |
| 168 |
N_("-i, --ignore-initial=SKIP skip first SKIP bytes of both inputs"), |
| 169 |
N_("-i, --ignore-initial=SKIP1:SKIP2 skip first SKIP1 bytes of FILE1 and\n" |
| 170 |
" first SKIP2 bytes of FILE2"), |
| 171 |
N_("-l, --verbose output byte numbers and differing byte values"), |
| 172 |
N_("-n, --bytes=LIMIT compare at most LIMIT bytes"), |
| 173 |
N_("-s, --quiet, --silent suppress all normal output"), |
| 174 |
N_(" --help display this help and exit"), |
| 175 |
N_("-v, --version output version information and exit"), |
| 176 |
nullptr |
| 177 |
}; |
| 178 |
|
| 179 |
static void |
| 180 |
usage (void) |
| 181 |
{ |
| 182 |
printf (_("Usage: %s [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]\n"), |
| 183 |
squote (0, program_name)); |
| 184 |
puts (_("Compare two files byte by byte.")); |
| 185 |
printf ("\n%s\n\n", |
| 186 |
_("The optional SKIP1 and SKIP2 specify the number of bytes to skip\n" |
| 187 |
"at the beginning of each file (zero by default).")); |
| 188 |
|
| 189 |
fputs (_("\ |
| 190 |
Mandatory arguments to long options are mandatory for short options too.\n\ |
| 191 |
"), stdout); |
| 192 |
for (char const *const *p = option_help_msgid; *p; p++) |
| 193 |
printf (" %s\n", _(*p)); |
| 194 |
printf ("\n%s\n\n%s\n%s\n", |
| 195 |
_("SKIP values may be followed by the following multiplicative suffixes:\n\ |
| 196 |
kB 1000, K 1024, MB 1,000,000, M 1,048,576,\n\ |
| 197 |
GB 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y."), |
| 198 |
_("If a FILE is '-' or missing, read standard input."), |
| 199 |
_("Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.")); |
| 200 |
emit_bug_reporting_address (); |
| 201 |
} |
| 202 |
|
| 203 |
int |
| 204 |
main (int argc, char **argv) |
| 205 |
{ |
| 206 |
exit_failure = EXIT_TROUBLE; |
| 207 |
initialize_main (&argc, &argv); |
| 208 |
set_program_name (argv[0]); |
| 209 |
setlocale (LC_ALL, ""); |
| 210 |
bindtextdomain (PACKAGE, LOCALEDIR); |
| 211 |
bindtextdomain ("gnulib", GNULIB_LOCALEDIR); |
| 212 |
textdomain (PACKAGE); |
| 213 |
c_stack_action (nullptr); |
| 214 |
xstdopen (); |
| 215 |
|
| 216 |
/* Parse command line options. */ |
| 217 |
|
| 218 |
for (int c; |
| 219 |
0 <= (c = getopt_long (argc, argv, shortopts, longopts, nullptr)); ) |
| 220 |
switch (c) |
| 221 |
{ |
| 222 |
case 'b': |
| 223 |
case 'c': /* 'c' is obsolescent as of diffutils 2.7.3 */ |
| 224 |
opt_print_bytes = true; |
| 225 |
break; |
| 226 |
|
| 227 |
case 'i': |
| 228 |
specify_ignore_initial (0, &optarg, ':'); |
| 229 |
if (*optarg++ == ':') |
| 230 |
specify_ignore_initial (1, &optarg, 0); |
| 231 |
else if (ignore_initial[1] < ignore_initial[0] || ignore_initial[0] < 0) |
| 232 |
ignore_initial[1] = ignore_initial[0]; |
| 233 |
break; |
| 234 |
|
| 235 |
case 'l': |
| 236 |
specify_comparison_type (type_all_diffs); |
| 237 |
break; |
| 238 |
|
| 239 |
case 'n': |
| 240 |
{ |
| 241 |
intmax_t n; |
| 242 |
strtol_error e = xstrtoimax (optarg, nullptr, 0, &n, valid_suffixes); |
| 243 |
if ((e & ~LONGINT_OVERFLOW) != LONGINT_OK || n < 0) |
| 244 |
try_help ("invalid --bytes value %s", quote (optarg)); |
| 245 |
bytes = MIN (bytes, n); |
| 246 |
} |
| 247 |
break; |
| 248 |
|
| 249 |
case 's': |
| 250 |
specify_comparison_type (type_status); |
| 251 |
break; |
| 252 |
|
| 253 |
case 'v': |
| 254 |
version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, |
| 255 |
AUTHORS, nullptr); |
| 256 |
check_stdout (); |
| 257 |
return EXIT_SUCCESS; |
| 258 |
|
| 259 |
case HELP_OPTION: |
| 260 |
usage (); |
| 261 |
check_stdout (); |
| 262 |
return EXIT_SUCCESS; |
| 263 |
|
| 264 |
default: |
| 265 |
try_help (nullptr, nullptr); |
| 266 |
} |
| 267 |
|
| 268 |
if (optind == argc) |
| 269 |
try_help ("missing operand after %s", quote (argv[argc - 1])); |
| 270 |
|
| 271 |
file[0] = argv[optind++]; |
| 272 |
file[1] = optind < argc ? argv[optind++] : "-"; |
| 273 |
|
| 274 |
for (int f = 0; f < 2 && optind < argc; f++) |
| 275 |
{ |
| 276 |
char *arg = argv[optind++]; |
| 277 |
specify_ignore_initial (f, &arg, 0); |
| 278 |
} |
| 279 |
|
| 280 |
if (optind < argc) |
| 281 |
try_help ("extra operand %s", quote (argv[optind])); |
| 282 |
|
| 283 |
for (int f = 0; f < 2; f++) |
| 284 |
{ |
| 285 |
/* Two files with the same name and offset are identical. |
| 286 |
But wait until we open the file once, for proper diagnostics. */ |
| 287 |
if (f && 0 <= ignore_initial[0] && ignore_initial[0] == ignore_initial[1] |
| 288 |
&& file_name_cmp (file[0], file[1]) == 0) |
| 289 |
return EXIT_SUCCESS; |
| 290 |
|
| 291 |
if (STREQ (file[f], "-")) |
| 292 |
{ |
| 293 |
file_desc[f] = STDIN_FILENO; |
| 294 |
if (O_BINARY && ! isatty (STDIN_FILENO)) |
| 295 |
set_binary_mode (STDIN_FILENO, O_BINARY); |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
file_desc[f] = open (file[f], O_RDONLY | O_BINARY | O_CLOEXEC); |
| 300 |
|
| 301 |
if (file_desc[f] < 0) |
| 302 |
{ |
| 303 |
if (comparison_type != type_status) |
| 304 |
error (0, errno, "%s", squote (0, file[f])); |
| 305 |
exit (EXIT_TROUBLE); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
if (fstat (file_desc[f], stat_buf + f) < 0) |
| 310 |
{ |
| 311 |
stat_buf[f].st_size = -2; |
| 312 |
#if HAVE_STRUCT_STAT_ST_BLKSIZE |
| 313 |
stat_buf[f].st_blksize = 8 * 1024; |
| 314 |
#endif |
| 315 |
} |
| 316 |
else |
| 317 |
stat_buf[f].st_size = stat_size (&stat_buf[f]); |
| 318 |
} |
| 319 |
|
| 320 |
/* If the files are the same and have the same file position, |
| 321 |
the contents are identical. */ |
| 322 |
|
| 323 |
if (-1 <= stat_buf[0].st_size && -1 <= stat_buf[1].st_size |
| 324 |
&& same_file (&stat_buf[0], &stat_buf[1]) |
| 325 |
&& file_position (0) == file_position (1)) |
| 326 |
return EXIT_SUCCESS; |
| 327 |
|
| 328 |
/* If output is redirected to the null device, we can avoid some of |
| 329 |
the work. */ |
| 330 |
|
| 331 |
if (comparison_type != type_status) |
| 332 |
{ |
| 333 |
struct stat outstat, nullstat; |
| 334 |
|
| 335 |
if (fstat (STDOUT_FILENO, &outstat) == 0 |
| 336 |
&& S_ISCHR (outstat.st_mode) |
| 337 |
&& stat (NULL_DEVICE, &nullstat) == 0 |
| 338 |
&& same_file (&outstat, &nullstat)) |
| 339 |
comparison_type = type_no_stdout; |
| 340 |
} |
| 341 |
|
| 342 |
/* If no output is needed, |
| 343 |
and both input descriptors are associated with plain files, |
| 344 |
and the file sizes are nonzero so they are not Linux /proc files, |
| 345 |
conclude that the files differ if they have different sizes |
| 346 |
and if more bytes will be compared than are in the smaller file. */ |
| 347 |
|
| 348 |
if (type_no_stdout <= comparison_type |
| 349 |
&& 0 <= stat_buf[0].st_size && S_ISREG (stat_buf[0].st_mode) |
| 350 |
&& 0 <= stat_buf[1].st_size && S_ISREG (stat_buf[1].st_mode)) |
| 351 |
{ |
| 352 |
off_t pos0 = file_position (0); |
| 353 |
if (0 <= pos0) |
| 354 |
{ |
| 355 |
off_t pos1 = file_position (1); |
| 356 |
if (0 <= pos1) |
| 357 |
{ |
| 358 |
off_t s0 = stat_buf[0].st_size - pos0; |
| 359 |
off_t s1 = stat_buf[1].st_size - pos1; |
| 360 |
if (s0 < 0) |
| 361 |
s0 = 0; |
| 362 |
if (s1 < 0) |
| 363 |
s1 = 0; |
| 364 |
if (s0 != s1 && MIN (s0, s1) < bytes) |
| 365 |
exit (EXIT_FAILURE); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
/* Guess a good block size for the files. */ |
| 371 |
|
| 372 |
idx_t blksize[2]; |
| 373 |
for (int f = 0; f < 2; f++) |
| 374 |
if (ST_BLKSIZE (stat_buf[0]) < 0 |
| 375 |
|| ckd_add (&blksize[f], ST_BLKSIZE (stat_buf[0]), 0)) |
| 376 |
blksize[f] = 0; |
| 377 |
buf_size = buffer_lcm (blksize[0], blksize[1], IDX_MAX - sizeof (word)); |
| 378 |
|
| 379 |
/* Allocate word-aligned buffers, with space for sentinels at the end. */ |
| 380 |
|
| 381 |
idx_t words_per_buffer = (buf_size + 2 * sizeof (word) - 1) / sizeof (word); |
| 382 |
buffer[0] = xinmalloc (words_per_buffer, 2 * sizeof (word)); |
| 383 |
buffer[1] = buffer[0] + words_per_buffer; |
| 384 |
|
| 385 |
int exit_status = cmp (); |
| 386 |
|
| 387 |
for (int f = 0; f < 2; f++) |
| 388 |
if (close (file_desc[f]) != 0) |
| 389 |
error (EXIT_TROUBLE, errno, "%s", squote (0, file[f])); |
| 390 |
if (exit_status != EXIT_SUCCESS && comparison_type < type_no_stdout) |
| 391 |
check_stdout (); |
| 392 |
exit (exit_status); |
| 393 |
} |
| 394 |
|
| 395 |
/* Compare the two files already open on 'file_desc[0]' and 'file_desc[1]', |
| 396 |
using 'buffer[0]' and 'buffer[1]'. |
| 397 |
Return EXIT_SUCCESS if identical, EXIT_FAILURE if different, |
| 398 |
>1 if error. */ |
| 399 |
|
| 400 |
static int |
| 401 |
cmp (void) |
| 402 |
{ |
| 403 |
word *buffer0 = buffer[0]; |
| 404 |
word *buffer1 = buffer[1]; |
| 405 |
char *buf0 = (char *) buffer0; |
| 406 |
char *buf1 = (char *) buffer1; |
| 407 |
|
| 408 |
/* For -l, the print width of the offset, a positive number. |
| 409 |
Otherwise, the negative of the comparison type. |
| 410 |
This portmanteauization pacifies gcc -Wmaybe-uninitialized. */ |
| 411 |
int offset_width; |
| 412 |
|
| 413 |
if (comparison_type == type_all_diffs) |
| 414 |
{ |
| 415 |
intmax_t byte_number_max = bytes; |
| 416 |
|
| 417 |
for (int f = 0; f < 2; f++) |
| 418 |
if (0 <= stat_buf[f].st_size && S_ISREG (stat_buf[f].st_mode)) |
| 419 |
{ |
| 420 |
off_t pos = file_position (f); |
| 421 |
if (0 <= pos) |
| 422 |
byte_number_max = MIN (byte_number_max, |
| 423 |
MAX (0, stat_buf[f].st_size - pos)); |
| 424 |
} |
| 425 |
|
| 426 |
for (offset_width = 1; (byte_number_max /= 10) != 0; offset_width++) |
| 427 |
continue; |
| 428 |
} |
| 429 |
else |
| 430 |
offset_width = -comparison_type; |
| 431 |
|
| 432 |
bool eof[2] = { false, false }; |
| 433 |
|
| 434 |
for (int f = 0; f < 2; f++) |
| 435 |
{ |
| 436 |
intmax_t ig = ignore_initial[f]; |
| 437 |
if (ig == 0) |
| 438 |
continue; |
| 439 |
|
| 440 |
if (0 <= file_position (f)) |
| 441 |
continue; /* lseek sufficed. */ |
| 442 |
|
| 443 |
if (! (0 <= ig && ig < TYPE_MAXIMUM (off_t)) |
| 444 |
&& -1 <= stat_buf[f].st_size && S_ISREG (stat_buf[f].st_mode)) |
| 445 |
{ |
| 446 |
/* When ignoring at least TYPE_MAXIMUM (off_t) bytes |
| 447 |
of a regular file, pretend to be at end of file, |
| 448 |
as lseeking to TYPE_MAXIMUM (off_t) might tickle a kernel bug, |
| 449 |
and lseeking to file end would race with a growing file. */ |
| 450 |
eof[f] = true; |
| 451 |
} |
| 452 |
else if (ig < 0) |
| 453 |
{ |
| 454 |
/* Report an error if asked to ignore more than |
| 455 |
INTMAX_MAX bytes of a non-regular file, |
| 456 |
as the actual number of bytes to ignore is not known. */ |
| 457 |
error (EXIT_TROUBLE, EOVERFLOW, "%s", squote (0, file[f])); |
| 458 |
} |
| 459 |
else |
| 460 |
{ |
| 461 |
/* Read and discard the ignored initial prefix. */ |
| 462 |
do |
| 463 |
{ |
| 464 |
idx_t bytes_to_read = MIN (ig, buf_size); |
| 465 |
ptrdiff_t r = block_read (file_desc[f], buf0, bytes_to_read); |
| 466 |
if (r != bytes_to_read) |
| 467 |
{ |
| 468 |
if (r < 0) |
| 469 |
error (EXIT_TROUBLE, errno, "%s", squote (0, file[f])); |
| 470 |
break; |
| 471 |
} |
| 472 |
ig -= r; |
| 473 |
} |
| 474 |
while (0 < ig); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
bool at_line_start = true; |
| 479 |
intmax_t line_number = 1; /* Line number (1...) of difference. */ |
| 480 |
intmax_t byte_number = 1; /* Byte number (1...) of difference. */ |
| 481 |
intmax_t remaining = bytes; /* Remaining bytes to compare, or -1. */ |
| 482 |
|
| 483 |
while (true) |
| 484 |
{ |
| 485 |
idx_t bytes_to_read = MIN (buf_size, remaining); |
| 486 |
remaining -= bytes_to_read; |
| 487 |
|
| 488 |
ptrdiff_t read0 = (eof[0] ? 0 |
| 489 |
: block_read (file_desc[0], buf0, bytes_to_read)); |
| 490 |
if (read0 < 0) |
| 491 |
error (EXIT_TROUBLE, errno, "%s", squote (0, file[0])); |
| 492 |
ptrdiff_t read1 = (eof[1] ? 0 |
| 493 |
: block_read (file_desc[1], buf1, bytes_to_read)); |
| 494 |
if (read1 < 0) |
| 495 |
error (EXIT_TROUBLE, errno, "%s", squote (0, file[1])); |
| 496 |
|
| 497 |
idx_t smaller = MIN (read0, read1); |
| 498 |
|
| 499 |
idx_t first_diff; /* Offset (0...) in buffers of 1st diff. */ |
| 500 |
|
| 501 |
/* Optimize the common case where the buffers are the same. */ |
| 502 |
if (memcmp (buf0, buf1, smaller) == 0) |
| 503 |
first_diff = smaller; |
| 504 |
else |
| 505 |
{ |
| 506 |
/* Insert sentinels for the block compare. */ |
| 507 |
if (read0 >= read1) |
| 508 |
buf1[read0] = 0x55; /* arbitrary */ |
| 509 |
if (read1 >= read0) |
| 510 |
buf0[read1] = 0x79; /* arbitrary and distinct from the above */ |
| 511 |
buf0[read0] = ~buf1[read0]; |
| 512 |
buf1[read1] = ~buf0[read1]; |
| 513 |
/* Ensure all bytes of a final word-read are initialized. */ |
| 514 |
memset (buf0 + read0 + 1, 0, |
| 515 |
sizeof (word) - read0 % sizeof (word) - 1); |
| 516 |
memset (buf1 + read1 + 1, 0, |
| 517 |
sizeof (word) - read1 % sizeof (word) - 1); |
| 518 |
|
| 519 |
first_diff = block_compare (buffer0, buffer1); |
| 520 |
} |
| 521 |
|
| 522 |
byte_number += first_diff; |
| 523 |
if (offset_width == -type_first_diff && first_diff != 0) |
| 524 |
{ |
| 525 |
line_number += count_newlines (buf0, first_diff); |
| 526 |
at_line_start = buf0[first_diff - 1] == '\n'; |
| 527 |
} |
| 528 |
|
| 529 |
int differing = 0; |
| 530 |
|
| 531 |
if (first_diff < smaller) |
| 532 |
{ |
| 533 |
switch (offset_width) |
| 534 |
{ |
| 535 |
case -type_first_diff: |
| 536 |
{ |
| 537 |
if (!opt_print_bytes) |
| 538 |
{ |
| 539 |
/* See POSIX for this format. This message is |
| 540 |
used only in the POSIX locale, so it need not |
| 541 |
be translated. */ |
| 542 |
static char const char_message[] = |
| 543 |
"%s %s differ: char %"PRIdMAX", line %"PRIdMAX"\n"; |
| 544 |
|
| 545 |
/* The POSIX rationale recommends using the word |
| 546 |
"byte" outside the POSIX locale. Some gettext |
| 547 |
implementations translate even in the POSIX |
| 548 |
locale if certain other environment variables |
| 549 |
are set, so use "byte" if a translation is |
| 550 |
available, or if outside the POSIX locale. */ |
| 551 |
static char const byte_msgid[] = |
| 552 |
N_("%s %s differ: byte %"PRIdMAX", line %"PRIdMAX"\n"); |
| 553 |
char const *byte_message = _(byte_msgid); |
| 554 |
bool use_byte_message = (byte_message != byte_msgid |
| 555 |
|| hard_locale_LC_MESSAGES ()); |
| 556 |
|
| 557 |
printf (use_byte_message ? byte_message : char_message, |
| 558 |
file[0], file[1], byte_number, line_number); |
| 559 |
} |
| 560 |
else |
| 561 |
{ |
| 562 |
unsigned char c0 = buf0[first_diff]; |
| 563 |
unsigned char c1 = buf1[first_diff]; |
| 564 |
char s0[5]; |
| 565 |
char s1[5]; |
| 566 |
sprintc (s0, c0); |
| 567 |
sprintc (s1, c1); |
| 568 |
printf (_("%s %s differ: byte %"PRIdMAX", line %"PRIdMAX |
| 569 |
" is %3o %s %3o %s\n"), |
| 570 |
file[0], file[1], byte_number, line_number, |
| 571 |
c0, s0, c1, s1); |
| 572 |
} |
| 573 |
} |
| 574 |
FALLTHROUGH; |
| 575 |
case -type_status: |
| 576 |
return EXIT_FAILURE; |
| 577 |
|
| 578 |
default: |
| 579 |
dassert (comparison_type == type_all_diffs); |
| 580 |
|
| 581 |
do |
| 582 |
{ |
| 583 |
unsigned char c0 = buf0[first_diff]; |
| 584 |
unsigned char c1 = buf1[first_diff]; |
| 585 |
if (c0 != c1) |
| 586 |
{ |
| 587 |
if (!opt_print_bytes) |
| 588 |
{ |
| 589 |
/* See POSIX for this format. */ |
| 590 |
printf ("%*"PRIdMAX" %3o %3o\n", |
| 591 |
offset_width, byte_number, c0, c1); |
| 592 |
} |
| 593 |
else |
| 594 |
{ |
| 595 |
char s0[5]; |
| 596 |
char s1[5]; |
| 597 |
sprintc (s0, c0); |
| 598 |
sprintc (s1, c1); |
| 599 |
printf ("%*"PRIdMAX" %3o %-4s %3o %s\n", |
| 600 |
offset_width, byte_number, c0, s0, c1, s1); |
| 601 |
} |
| 602 |
} |
| 603 |
byte_number++; |
| 604 |
first_diff++; |
| 605 |
} |
| 606 |
while (first_diff < smaller); |
| 607 |
|
| 608 |
differing = -1; |
| 609 |
break; |
| 610 |
|
| 611 |
case -type_no_stdout: |
| 612 |
differing = 1; |
| 613 |
break; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
if (read0 != read1) |
| 618 |
{ |
| 619 |
/* POSIX says that each of these format strings must be |
| 620 |
"cmp: EOF on %s", optionally followed by a blank and |
| 621 |
extra text sans newline, then terminated by "\n". */ |
| 622 |
if (differing <= 0 && offset_width != -type_status) |
| 623 |
fprintf (stderr, |
| 624 |
_(byte_number == 1 |
| 625 |
? N_("cmp: EOF on %s which is empty\n") |
| 626 |
: offset_width != -type_first_diff |
| 627 |
? N_("cmp: EOF on %s after byte %"PRIdMAX"\n") |
| 628 |
: at_line_start |
| 629 |
? N_("cmp: EOF on %s after byte %"PRIdMAX"," |
| 630 |
" line %"PRIdMAX"\n") |
| 631 |
: N_("cmp: EOF on %s after byte %"PRIdMAX"," |
| 632 |
" in line %"PRIdMAX"\n")), |
| 633 |
quote (file[read1 < read0]), |
| 634 |
byte_number - 1, line_number - at_line_start); |
| 635 |
return EXIT_FAILURE; |
| 636 |
} |
| 637 |
|
| 638 |
if (0 < differing || read0 != buf_size) |
| 639 |
return differing == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
/* Compare two blocks of memory P0 and P1 until they differ. |
| 644 |
If the blocks are not guaranteed to be different, put sentinels at the ends |
| 645 |
of the blocks before calling this function. |
| 646 |
|
| 647 |
Return the offset of the first byte that differs. */ |
| 648 |
|
| 649 |
static idx_t |
| 650 |
block_compare (word const *p0, word const *p1) |
| 651 |
{ |
| 652 |
word const *l0, *l1; |
| 653 |
char const *c0, *c1; |
| 654 |
|
| 655 |
/* Find the rough position of the first difference by reading words, |
| 656 |
not bytes. */ |
| 657 |
|
| 658 |
for (l0 = p0, l1 = p1; *l0 == *l1; l0++, l1++) |
| 659 |
continue; |
| 660 |
|
| 661 |
/* Find the exact differing position (endianness independent). */ |
| 662 |
|
| 663 |
for (c0 = (char const *) l0, c1 = (char const *) l1; |
| 664 |
*c0 == *c1; |
| 665 |
c0++, c1++) |
| 666 |
continue; |
| 667 |
|
| 668 |
return c0 - (char const *) p0; |
| 669 |
} |
| 670 |
|
| 671 |
/* Return the number of newlines in BUF, of size BUFSIZE, |
| 672 |
where BUF[NBYTES] is available for use as a sentinel. */ |
| 673 |
|
| 674 |
static idx_t |
| 675 |
count_newlines (char *buf, idx_t bufsize) |
| 676 |
{ |
| 677 |
idx_t count = 0; |
| 678 |
char *lim = buf + bufsize; |
| 679 |
char ch = *lim; |
| 680 |
*lim = '\n'; |
| 681 |
for (char *p = buf; (p = rawmemchr (p, '\n')) != lim; p++) |
| 682 |
count++; |
| 683 |
*lim = ch; |
| 684 |
return count; |
| 685 |
} |
| 686 |
|
| 687 |
/* Put into BUF the unsigned char C, making unprintable bytes |
| 688 |
visible by quoting like cat -t does. */ |
| 689 |
|
| 690 |
static void |
| 691 |
sprintc (char *buf, unsigned char c) |
| 692 |
{ |
| 693 |
if (! c_isprint (c)) |
| 694 |
{ |
| 695 |
if (c >= 128) |
| 696 |
{ |
| 697 |
*buf++ = 'M'; |
| 698 |
*buf++ = '-'; |
| 699 |
c -= 128; |
| 700 |
} |
| 701 |
if (c < 32) |
| 702 |
{ |
| 703 |
*buf++ = '^'; |
| 704 |
c += 64; |
| 705 |
} |
| 706 |
else if (c == 127) |
| 707 |
{ |
| 708 |
*buf++ = '^'; |
| 709 |
c = '?'; |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
*buf++ = c; |
| 714 |
*buf = 0; |
| 715 |
} |
| 716 |
|
| 717 |
/* Position file F to ignore_initial[F] bytes from its initial position, |
| 718 |
and yield its new position. Return a negative number on failure. |
| 719 |
Do not report an error on failure, as lseek is generally a no-op |
| 720 |
on devices that cannot seek. Don't try more than once. */ |
| 721 |
|
| 722 |
static off_t |
| 723 |
file_position (int f) |
| 724 |
{ |
| 725 |
/* The initial position of input file F, and whether that position has |
| 726 |
been determined. The position is -1 if it could not be determined. */ |
| 727 |
static bool positioned[2]; |
| 728 |
static off_t position[2]; |
| 729 |
|
| 730 |
if (! positioned[f]) |
| 731 |
{ |
| 732 |
positioned[f] = true; |
| 733 |
off_t pos = ignore_initial[f]; |
| 734 |
position[f] = (0 <= pos && pos <= TYPE_MAXIMUM (off_t) |
| 735 |
? lseek (file_desc[f], pos, SEEK_CUR) |
| 736 |
: -1); |
| 737 |
} |
| 738 |
return position[f]; |
| 739 |
} |