Page MenuHomeFreeBSD

cmp: fix -s (silent) when used with skip offsets

Authored by emaste on Jan 9 2021, 10:02 PM.



-s causes cmp to print nothing for differing files, for use when only the exit status is of interest.

-z compares the file size first, for regular files, and fails the comparison early if they do not match.

Prior to this change -s implied -z as an optimization, but this is not valid when file offsets are specified. Now, enable the -z optimization for -s only if both skip arguments are not provided / 0.

PR: 252542
Reported by: William Ahern
Sponsored by: The FreeBSD Foundation

Diff Detail

R10 FreeBSD src repository
Automatic diff as part of commit; lint not applicable.
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

emaste created this revision.
emaste added a parent revision: D28072: cmp: use C99 bool.

I first switched the flags to C99 bool in D28072

Fixes: 3e6902efc802ab57fc4e9bf798f2d271b152e7f9

-z basically ignores user-specified file offsets:

echo -n '1234567890' > a
echo -n 'abc567890' > b
cmp -z a b 4 3 => a and b differ

I'm not sure if it's really a bug or not (if not some clarification in the man page would be good), but in principle you could retain the optimization if -z instead checks for size1 - skip1 != size2 - skip2.

This revision is now accepted and ready to land.Jan 10 2021, 8:40 PM

I'm not sure if it's really a bug or not

Agreed, the documentation is not quite clear on this although the code matches the interpretation I would take from the man page. It's probably worth changing the optimization as you suggest, and making this explicit in the man page... I might take a look at it later.