Skip to content

cmp: accept unit suffixes in --bytes - #309

Open
jayhemnani9910 wants to merge 1 commit into
uutils:mainfrom
jayhemnani9910:cmp-bytes-units
Open

jayhemnani9910 wants to merge 1 commit into
uutils:mainfrom
jayhemnani9910:cmp-bytes-units

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

-n/--bytes only takes a plain number, so anything with a unit fails before comparing:

$ head -c 3000 /dev/zero > z1; cp z1 z2; printf '\1' | dd of=z2 bs=1 seek=1500 conv=notrunc status=none
$ diffutils cmp -n 1K z1 z2; echo $?
cmp: invalid --bytes value '1K'
2
$ /usr/bin/cmp -n 1K z1 z2; echo $?    # GNU cmp 3.12
0
$ /usr/bin/cmp -n 2K z1 z2
z1 z2 differ: byte 1501, line 1

GNU cmp takes the same suffixes for --bytes as for --ignore-initial, and the code already knew it (the TODO on the 1K test, and the comment on BytesLimitU64 about limits like 1Y).

The --ignore-initial parser becomes parse_size, which takes the option name for its error message. --bytes uses it, and parse_skip is now a thin wrapper, so its callers don't change. The test that expected --bytes=1K to fail now checks -n 1K, --bytes=1kB and --bytes=2M, and keeps a failure case for 1x.

Tests: the new cases fail on main. cargo test: 285 passed, same as main. cargo fmt --check is clean, and cargo clippy --all-targets gives the same warnings as on main. Against GNU cmp 3.12 with those two files, -n with 1K, 1kB, 2K, 1M, 1500K, 1T, 1Y, 1500 and 1499 gives the same result and exit code. 1b, 1x, 1Ki and -1 give the same error. GNU also takes 1k, 1KB, 1KiB and hex, and reads a leading 0 as octal, for both options; this parser does neither (it reads 010 as ten), and I left that for a separate change.

-n/--bytes only took a plain number, so "cmp -n 1K a b" failed with
"invalid --bytes value", while GNU cmp takes the same suffixes there as
for --ignore-initial. Share the --ignore-initial parser, with the option
name in its error message, and turn the test that expected 1K to fail
into cases for -n 1K, --bytes=1kB and --bytes=2M.
Copilot AI balanced review requested due to automatic review settings October 20, 2026 05:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants