Skip to content

wc: derive column width from the size of stdin - #14187

Open
Socialpranker wants to merge 2 commits into
uutils:mainfrom
Socialpranker:wc-stdin-width
Open

Socialpranker wants to merge 2 commits into
uutils:mainfrom
Socialpranker:wc-stdin-width

Conversation

@Socialpranker

Copy link
Copy Markdown
Contributor

wc pads its counts to a width derived from the total size of its inputs, but
stdin was always treated as a stream of unknown length, even when it is a
regular file whose size fstat reports up front. The same 839-byte file
therefore printed differently depending on how it was handed to wc:

$ wc < 839-byte-file
     18      20     839
$ wc 839-byte-file
 18  20 839 839-byte-file

GNU wc prints 18 20 839 in both cases; it only falls back to the minimum
width when the size genuinely is not known in advance.

This PR takes the size of stdin from its metadata when stdin is a regular file
and feeds it into the existing width computation, so:

  • wc < file matches wc file;
  • a - argument contributes the size of stdin to the total, the way a named
    file does (wc - other-file < file widens to fit size(file) + size(other));
  • pipes, terminals and character devices are unchanged — cat file | wc and
    wc < /dev/null still use the minimum width of 7;
  • a regular file of size zero still yields width 1, matching how a zero-byte
    named file behaves.

The digit-width calculation that was inline in compute_number_width is pulled
out into a small width_of helper so both the stdin-only and the mixed-input
paths share it.

Testing: new test_stdin_size_dictates_width in tests/by-util/test_wc.rs
covers the three cases (regular file on stdin, the same content piped in, and
- mixed with a named file). It fails on the current main and passes with
this change. The full wc suite is green (58 passed), cargo fmt --check and
cargo clippy -p uu_wc --all-targets are clean.

Behaviour was established by observing GNU wc's output on a Debian system —
no GNU source was consulted.

@Socialpranker

Copy link
Copy Markdown
Contributor Author

Side-by-side check against GNU wc (Debian, coreutils 9.x) with the patched binary built from this branch — all eight cases that differed before now match byte for byte:

OK   wc < p839            → [ 18  20 839]
OK   wc - < p839          → [ 18  20 839 -]
OK   wc - p839 < p839     → [  18   20  839 -|  18   20  839 p839|  36   40 1678 total]
OK   wc < /dev/null       → [      0       0       0]
OK   wc < big             → [ 156 1129 8011]
OK   wc -l < p839         → [18]
OK   wc < /proc/cpuinfo   → [45 310 1715]
OK   cat p839 | wc        → [     18      20     839]

/proc/cpuinfo is the interesting one: it is a regular file of size 0, so the width stays 1 even though 1715 bytes are read — same as GNU.

@github-actions

github-actions Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/env/env is no longer failing!
Congrats! The gnu test tests/misc/usage_vs_getopt is no longer failing!

@sylvestre

Copy link
Copy Markdown
Contributor

A bunch of jobs are failing

@sylvestre

Copy link
Copy Markdown
Contributor

Is there a bug somewhere about this issue?

@Socialpranker

Copy link
Copy Markdown
Contributor Author

No, there's no filed issue — I found it by diffing uutils against GNU coreutils on the same inputs. The reproducer is in the description above:

$ wc < 839-byte-file
     18      20     839
$ wc 839-byte-file
 18  20 839 839-byte-file

GNU prints 18 20 839 in both cases, uutils widens to the minimum-width fallback in the first. Happy to open an issue for it if you'd prefer the report tracked separately.

The red CI here isn't from this change: the same jobs (wasm32-wasip1, i686-pc-windows-msvc, and others) are failing on main right now.

@Socialpranker

Copy link
Copy Markdown
Contributor Author

rebased. had to update test_count_bytes_from_repeated_redirected_stdin, GNU prints ' 6 -' there, and skipped it on wasi

A regular file redirected onto stdin has a size known up front, so it
sets the column width the same way a named file does, as GNU wc does.
Pipes and other streams keep the minimum-width fallback.
@Socialpranker

Copy link
Copy Markdown
Contributor Author

Rebased onto main to rerun CI. None of the earlier failures were in wc: test_pr::test_missing_non_utf8_input_does_not_read_stdin (broken pipe on Linux) and test_shred::test_shred_dash_rejects_stdout_pipe (macOS) pass on main, and the Tests and MSYS failures (test_install, test_split, missing stdbuf.dll) are identical in #16345.

This branch has not been deployed

No deployments
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