wc: derive column width from the size of stdin - #14187
Open
Socialpranker wants to merge 2 commits into
Open
Conversation
When stdin is a regular file its size is known before reading, but the
counts were still padded to the fixed minimum width of 7:
$ wc < 839-byte-file
18 20 839
$ wc 839-byte-file
18 20 839 839-byte-file
The width is now derived from the size of stdin whenever fstat reports a
regular file, which also makes "-" contribute its size to the total for
mixed inputs. Streams whose length is not known in advance (pipes,
terminals, character devices) keep the minimum width.
$ wc < 839-byte-file
18 20 839
$ cat 839-byte-file | wc
18 20 839
Author
|
Side-by-side check against GNU
|
|
GNU testsuite comparison: |
Contributor
|
A bunch of jobs are failing |
`at_and_ucmd!` is imported under `#[cfg(unix)]` in this file, so the test failed to compile on the windows and wasm targets.
Contributor
|
Is there a bug somewhere about this issue? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
wcpads its counts to a width derived from the total size of its inputs, butstdin was always treated as a stream of unknown length, even when it is a
regular file whose size
fstatreports up front. The same 839-byte filetherefore printed differently depending on how it was handed to
wc:GNU
wcprints18 20 839in both cases; it only falls back to the minimumwidth 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 < filematcheswc file;-argument contributes the size of stdin to the total, the way a namedfile does (
wc - other-file < filewidens to fitsize(file) + size(other));cat file | wcandwc < /dev/nullstill use the minimum width of 7;named file behaves.
The digit-width calculation that was inline in
compute_number_widthis pulledout into a small
width_ofhelper so both the stdin-only and the mixed-inputpaths share it.
Testing: new
test_stdin_size_dictates_widthintests/by-util/test_wc.rscovers the three cases (regular file on stdin, the same content piped in, and
-mixed with a named file). It fails on the currentmainand passes withthis change. The full
wcsuite is green (58 passed),cargo fmt --checkandcargo clippy -p uu_wc --all-targetsare clean.Behaviour was established by observing GNU
wc's output on a Debian system —no GNU source was consulted.