Repository navigation
ls: honor COLUMNS env when stdout is not a terminal; -w 0 = no limit (fixes #1504) - #1505
caomengxuan666 wants to merge 1 commit into
Conversation
GNU coreutils 9.4 ls.c decode_switches() (ls.c:2270-2307) resolves the
output width as: the -w/--width option, then the TIOCGWINSZ width on
stdout (ls.c:2283-2286), then the COLUMNS environment variable parsed
by decode_line_length() (ls.c:1866-1884, consulted only when the ioctl
is unavailable, ls.c:2292-2303), then 80. Values 0 or too large to fit
mean "no limit" (one unwrapped line via print_with_separator,
ls.c:4142-4153 and ls.c:5227-5233); invalid values emit "ignoring
invalid width in environment variable COLUMNS" (ls.c:2299-2302) and
are ignored.
WinuxCmd ls returned a hardcoded 80 whenever stdout was not a console
and never read COLUMNS; -w 0 was treated as "unset" instead of "no
limit". Implement the full chain in ls.cpp (get_terminal_width /
decode_line_length / resolve_output_width) routed through the grid and
comma printers; dir/vdir dispatch into the same pipeline and inherit
the fix.
Verified against WSL GNU coreutils 9.4 on a 26-entry mixed-length
fixture: stdout byte-identical for COLUMNS in {160, 40, unset, 0, abc,
overflow, -40, " 40", 40x, 0x50, 010, 100000} with -C, plus -w 120
beating COLUMNS=40 and -w 0 meaning no limit; stderr byte-identical
for the invalid-width warning (UTF-8 quotes, as under C.UTF-8). The
live-terminal cell is unchanged: verified via ConPTY that COLUMNS=160
at an actual width of 80 still lays out at 80, matching the GNU
ioctl-over-COLUMNS priority. 14 new unit tests; ls/dir/vdir suites
pass (187 tests).
Fixes #1504
Captain review — CAN MERGE (for #1504)I read the full diff (2 files, +322/−24) and checked it against the
CI: the differential run's first Dedup note: PR #1534 also touches the #1504 ls face as one of four |
|
Closed as superseded by the #1534 merge (c418284, 2026-11-07): #1534 landed its own ls COLUMNS face for #1504, so this PR's face no longer applies on top of the merged tree. Timestamp check: this PR opened 2026-11-02, predating #1534's ls hunk — the overlap is same-side duplication, not a contest; the captain's dedup review (comment on #1534) compared both faces in full and recommended this one for GNU fidelity, but the merge as-is is the call that was made. Thanks for the GNU-faithful implementation (base-0 COLUMNS parsing, 8 regression nails) — it stays the reference: the five divergences of the merged face are tracked as known residuals in #1536, and this PR's diff + nails are the shape a follow-up fix should converge to. #1504 itself is closed by the #1534 merge with that residual note. |
|
Superseded by the #1534 merge — see the evidence comment just posted for the full record. |
Fixes #1504.
GNU rule implemented (coreutils 9.4 source, cited per repo policy)
src/ls.cdecode_switches():-w/--widthoption — ls.c:2270 (and0= "no limit", ls.c:1871)TIOCGWINSZon stdout when a tty withws_col > 0— ls.c:2283-2286COLUMNSenv, only when 2 yielded nothing — ls.c:2292-2303, parsed bydecode_line_length()— ls.c:1866-1884 (base-0 number: leading spaces OK,0xhex / leading-0 octal accepted;0and out-of-range = "no limit";-, trailing junk, no digits = invalid → warn and ignore)"No limit" routes
-C/-xthroughprint_with_separator(' ')(ls.c:4142-4153,ls.c:5227-5233): one line, entries joined by exactly two spaces, no tab
alignment.
Changes
src/commands/ls.cpp:get_terminal_width()now implements the fullpriority chain (live
srWindowwidth → COLUMNS fallback withdecode_line_lengthsemantics and the GNU invalid-value diagnostic,emitted once per process → 80). New
resolve_output_width(ctx)unifiesthe three width-resolution call sites and makes an explicit
-w 0/--width=0mean "no limit" instead of "unset".print_gridgainsthe GNU no-limit branch (two-space single line). A column-count clamp in
the (currently unreferenced)
print_columnskeeps a no-limit width fromallocating an unbounded vector.
dir/vdirdispatch into the ls pipelineand inherit everything.
tests/unit/ls/ls_unit_test.cpp: 14 new tests covering COLUMNS used whenpiped,
0/overflow = no limit, invalid → GNU warning text (byte-exact,UTF-8 quotes) + 80 fallback, option-beats-env,
-w 0, empty COLUMNSignored, and
dirinheritance.Verification
stdout: byte-identical for COLUMNS ∈ {160, 40, unset, 0, abc,
999999999999999999999999, -40, " 40", 40x, 0x50, 010, 100000} with
-C,plus
-w 120beating COLUMNS=40 and-w 0= single line. Warning stderrbyte-identical for GNU invoked as
ls(the/usr/bin/ls:-prefixdifference is only program-name, matching argv[0] either way).
actual width of 80 still lays out at 80 (GNU's ioctl-over-COLUMNS
priority); shrink transitions 80→160→80 were already correct and stay so.
ctest -R '^(ls|dir|vdir)\.'→ 187/187 pass.shell-root and stdin-state tests) reproduce on this machine independent of
this branch — environment-bound, not touched by this diff.
Known micro-gaps left out of scope (documented in #1504)
ls -w 5000000000: the option framework's INT parse rejects it; GNUaccepts out-of-range
-was "no limit".COLUMNS=abc ls -1 --color=alwaysdoes not warn here (GNU consults widthwhenever
--coloris active even in non-wrapping formats).'underLC_ALL=C(WinuxCmd has no locale engine).Evidence artifacts:
D:/repo/nbbuild/evidence/ls-width/(GNU referenceoutputs, fixed-binary outputs, PTY transcripts, comparison scripts).