From 8fd5107945595e5150d11fa41b297db7ebea418a Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 03:02:09 +0530 Subject: [PATCH 1/4] cmp: size the -l offset column from the regular-file operand when the other is stdin The offset width was only narrowed when both operands could be stat'ed, so 'cmp -l - file' used the 19-column u64::MAX width. Like GNU cmp, use the size of every operand that is a regular file (minus its skip) and ignore standard input, pipes and devices. Fixes #348 --- src/cmp.rs | 47 ++++++++++++++++++++++++++++----------- tests/by-util/test_cmp.rs | 24 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/cmp.rs b/src/cmp.rs index cdd0b5b9..917d86de 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -296,6 +296,26 @@ fn prepare_reader( Ok(reader) } +/// Returns the size of `path` when it is a regular file, and `None` for standard input +/// (`-`), pipes, devices and paths that cannot be stat'ed. +fn regular_file_size(path: &OsString) -> Option { + if path == "-" { + return None; + } + let meta = fs::metadata(path).ok()?; + if !meta.is_file() { + return None; + } + + #[cfg(not(target_os = "windows"))] + let size = meta.len(); + + #[cfg(target_os = "windows")] + let size = meta.file_size(); + + Some(size) +} + #[derive(Debug)] pub enum Cmp { Equal, @@ -308,24 +328,25 @@ pub fn cmp(params: &Params) -> Result { let mut offset_width = params.max_bytes.unwrap_or(BytesLimitU64::MAX); - if let (Ok(a_meta), Ok(b_meta)) = (fs::metadata(¶ms.from), fs::metadata(¶ms.to)) { - #[cfg(not(target_os = "windows"))] - let (a_size, b_size) = (a_meta.len(), b_meta.len()); - - #[cfg(target_os = "windows")] - let (a_size, b_size) = (a_meta.file_size(), b_meta.file_size()); + let a_size = regular_file_size(¶ms.from); + let b_size = regular_file_size(¶ms.to); - // If the files have different sizes, we already know they are not identical. If we have not - // been asked to show even the first difference, we can quit early. + // If the files have different sizes, we already know they are not identical. If we have not + // been asked to show even the first difference, we can quit early. + if let (Some(a_size), Some(b_size)) = (a_size, b_size) { if params.quiet && a_size != b_size { return Ok(Cmp::Different); } + } - // The offsets count from the end of the skipped bytes. - let a_left = a_size.saturating_sub(params.skip_a.unwrap_or(0)); - let b_left = b_size.saturating_sub(params.skip_b.unwrap_or(0)); - let smaller = cmp::min(a_left, b_left) as BytesLimitU64; - offset_width = cmp::min(smaller, offset_width); + // Like GNU cmp, size the offset column from every operand that is a regular file, so that + // standard input or a pipe on one side does not widen it to the u64::MAX width. The offsets + // count from the end of the skipped bytes. + for (size, skip) in [(a_size, params.skip_a), (b_size, params.skip_b)] { + if let Some(size) = size { + let left = size.saturating_sub(skip.unwrap_or(0)) as BytesLimitU64; + offset_width = cmp::min(left, offset_width); + } } let offset_width = 1 + offset_width.checked_ilog10().unwrap_or(1) as usize; diff --git a/tests/by-util/test_cmp.rs b/tests/by-util/test_cmp.rs index 020a941f..b64b3fb8 100644 --- a/tests/by-util/test_cmp.rs +++ b/tests/by-util/test_cmp.rs @@ -55,6 +55,30 @@ fn cmp_verbose_offset_width_after_skip() -> Result<(), Box Result<(), Box> { + let tmp_dir = tempdir()?; + let b_path = tmp_dir.path().join("b"); + File::create(&b_path)?.write_all(b"abcdeXghiY")?; + + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.env("LC_ALL", "C"); + cmd.arg("cmp").arg("-l").arg("-").arg(&b_path); + cmd.write_stdin("abcdefghij"); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::eq(" 6 146 130\n10 152 131\n")); + + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.env("LC_ALL", "C"); + cmd.arg("cmp").arg("-l").arg(&b_path).arg("-"); + cmd.write_stdin("abcdefghij"); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::eq(" 6 130 146\n10 131 152\n")); + Ok(()) +} + #[test] fn cmp_incompatible_params() -> Result<(), Box> { let mut cmd = cargo_bin_cmd!("diffutils"); From d1867e7467d6c1581b3fce56ed903c3196bffa43 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 03:56:18 +0530 Subject: [PATCH 2/4] cmp: use Metadata::len on every platform for regular_file_size Metadata::len() is cross-platform, so the Windows-only file_size() branch and its MetadataExt import are not needed. --- src/cmp.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/cmp.rs b/src/cmp.rs index 917d86de..7dea4a3a 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -17,9 +17,6 @@ use std::os::fd::{AsRawFd, FromRawFd}; #[cfg(unix)] use std::os::unix::fs::MetadataExt; -#[cfg(target_os = "windows")] -use std::os::windows::fs::MetadataExt; - /// for --bytes, so really large number limits can be expressed, like 1Y. pub type BytesLimitU64 = u64; // ignore initial is currently limited to u64, as take(skip) is used. @@ -307,13 +304,7 @@ fn regular_file_size(path: &OsString) -> Option { return None; } - #[cfg(not(target_os = "windows"))] - let size = meta.len(); - - #[cfg(target_os = "windows")] - let size = meta.file_size(); - - Some(size) + Some(meta.len()) } #[derive(Debug)] From 7a2021f73e4fd9c6d5ef844800fb1e16ee5de8ba Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 19:22:27 +0530 Subject: [PATCH 3/4] cmp: test -s with a pipe or device operand /dev/stdin as a pipe and /dev/null as a character device now skip the size-based -s early exit and are compared by content, on either side. Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- tests/by-util/test_cmp.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/by-util/test_cmp.rs b/tests/by-util/test_cmp.rs index b64b3fb8..c5afd210 100644 --- a/tests/by-util/test_cmp.rs +++ b/tests/by-util/test_cmp.rs @@ -79,6 +79,43 @@ fn cmp_verbose_offset_width_stdin() -> Result<(), Box> { Ok(()) } +#[test] +#[cfg(unix)] +fn cmp_silent_pipe_operand_is_read() -> Result<(), Box> { + // /dev/stdin is a pipe here, so its metadata reports a size of 0. That size must not be + // used for the -s early exit, or equal input is reported as different. + let tmp_dir = tempdir()?; + let b_path = tmp_dir.path().join("b"); + File::create(&b_path)?.write_all(b"abcdefghij")?; + + for args in [["/dev/stdin", "b"], ["b", "/dev/stdin"]] { + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.current_dir(tmp_dir.path()); + cmd.arg("cmp").arg("-s").args(args); + cmd.write_stdin("abcdefghij"); + cmd.assert().code(predicate::eq(0)).stdout(predicate::str::is_empty()); + + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.current_dir(tmp_dir.path()); + cmd.arg("cmp").arg("-s").args(args); + cmd.write_stdin("abcdeXghij"); + cmd.assert().code(predicate::eq(1)).stdout(predicate::str::is_empty()); + } + + // A character device on one side: /dev/null is empty, so it equals an empty file and + // differs from a non-empty one. + let empty_path = tmp_dir.path().join("empty"); + File::create(&empty_path)?; + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.arg("cmp").arg("-s").arg("/dev/null").arg(&empty_path); + cmd.assert().code(predicate::eq(0)); + + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.arg("cmp").arg("-s").arg("/dev/null").arg(&b_path); + cmd.assert().code(predicate::eq(1)); + Ok(()) +} + #[test] fn cmp_incompatible_params() -> Result<(), Box> { let mut cmd = cargo_bin_cmd!("diffutils"); From 3a46b4a08485699fe409d669258604bf0d34bb12 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 19:22:48 +0530 Subject: [PATCH 4/4] cmp: rustfmt the -s pipe test Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- tests/by-util/test_cmp.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/by-util/test_cmp.rs b/tests/by-util/test_cmp.rs index c5afd210..dc1009d0 100644 --- a/tests/by-util/test_cmp.rs +++ b/tests/by-util/test_cmp.rs @@ -93,13 +93,17 @@ fn cmp_silent_pipe_operand_is_read() -> Result<(), Box> { cmd.current_dir(tmp_dir.path()); cmd.arg("cmp").arg("-s").args(args); cmd.write_stdin("abcdefghij"); - cmd.assert().code(predicate::eq(0)).stdout(predicate::str::is_empty()); + cmd.assert() + .code(predicate::eq(0)) + .stdout(predicate::str::is_empty()); let mut cmd = cargo_bin_cmd!("diffutils"); cmd.current_dir(tmp_dir.path()); cmd.arg("cmp").arg("-s").args(args); cmd.write_stdin("abcdeXghij"); - cmd.assert().code(predicate::eq(1)).stdout(predicate::str::is_empty()); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::str::is_empty()); } // A character device on one side: /dev/null is empty, so it equals an empty file and