Skip to content

ls: honor COLUMNS env when stdout is not a terminal; -w 0 = no limit (fixes #1504) - #1505

Closed
caomengxuan666 wants to merge 1 commit into
mainfrom
fix/iss-1504-ls-columns-width
Closed

caomengxuan666 wants to merge 1 commit into
mainfrom
fix/iss-1504-ls-columns-width

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1504.

GNU rule implemented (coreutils 9.4 source, cited per repo policy)

src/ls.c decode_switches():

  1. -w/--width option — ls.c:2270 (and 0 = "no limit", ls.c:1871)
  2. TIOCGWINSZ on stdout when a tty with ws_col > 0 — ls.c:2283-2286
  3. COLUMNS env, only when 2 yielded nothing — ls.c:2292-2303, parsed by
    decode_line_length() — ls.c:1866-1884 (base-0 number: leading spaces OK,
    0x hex / leading-0 octal accepted; 0 and out-of-range = "no limit";
    -, trailing junk, no digits = invalid → warn and ignore)
  4. default 80 — ls.c:2307

"No limit" routes -C/-x through print_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 full
    priority chain (live srWindow width → COLUMNS fallback with
    decode_line_length semantics and the GNU invalid-value diagnostic,
    emitted once per process → 80). New resolve_output_width(ctx) unifies
    the three width-resolution call sites and makes an explicit
    -w 0/--width=0 mean "no limit" instead of "unset". print_grid gains
    the GNU no-limit branch (two-space single line). A column-count clamp in
    the (currently unreferenced) print_columns keeps a no-limit width from
    allocating an unbounded vector. dir/vdir dispatch into the ls pipeline
    and inherit everything.
  • tests/unit/ls/ls_unit_test.cpp: 14 new tests covering COLUMNS used when
    piped, 0/overflow = no limit, invalid → GNU warning text (byte-exact,
    UTF-8 quotes) + 80 fallback, option-beats-env, -w 0, empty COLUMNS
    ignored, and dir inheritance.

Verification

  • WSL GNU coreutils 9.4 as oracle, 26-entry mixed-length fixture, redirected
    stdout: byte-identical for COLUMNS ∈ {160, 40, unset, 0, abc,
    999999999999999999999999, -40, " 40", 40x, 0x50, 010, 100000} with -C,
    plus -w 120 beating COLUMNS=40 and -w 0 = single line. Warning stderr
    byte-identical for GNU invoked as ls (the /usr/bin/ls:-prefix
    difference is only program-name, matching argv[0] either way).
  • Live-terminal cell unchanged: pywinpty ConPTY probe with COLUMNS=160 at an
    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.
  • Full-suite note: 12 failures in unrelated commands (nohup/which/sed/...
    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; GNU
    accepts out-of-range -w as "no limit".
  • COLUMNS=abc ls -1 --color=always does not warn here (GNU consults width
    whenever --color is active even in non-wrapping formats).
  • The warning always uses UTF-8 ‘’ quotes; GNU switches to ASCII ' under
    LC_ALL=C (WinuxCmd has no locale engine).

Evidence artifacts: D:/repo/nbbuild/evidence/ls-width/ (GNU reference
outputs, fixed-binary outputs, PTY transcripts, comparison scripts).

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
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — CAN MERGE (for #1504)

I read the full diff (2 files, +322/−24) and checked it against the
GNU coreutils 9.4 rule the PR cites. The implementation is faithful on
every edge I probed:

  • decode_line_length implements real base-0 semantics (decimal,
    0x hex, leading-0 octal), leading whitespace and + accepted;
    0 and overflow both mean "no limit"; -, trailing junk and
    digit-less values are invalid.
  • -w/--width wins, and -w 0 means no limit — it does not fall
    through to COLUMNS (pinned by ls_width_option_zero_means_no_limit).
  • COLUMNS: empty is ignored silently (ls.c:2295 if (p && *p)),
    invalid warns once per process with GNU's UTF-8 quotes and falls
    back to 80, overflow means no limit (24-digit value pinned).
  • No-limit rendering matches both layout paths: print_grid joins
    with exactly two spaces on one line, print_columns bounds the
    column count so an INT_MAX width yields one row; dir inherits
    the chain.
  • 8 new tests pin exactly these boundaries.

CI: the differential run's first oracle failure was the savannah
gnulib clone returning HTTP 500 (infrastructure, same signature seen
on other PRs); after a re-run of the failed job the whole run —
oracle, baseline, baseline-growth, compare — is green, and the CMake
run (all builds + i18n-drift + ctest) was already green.

Dedup note: PR #1534 also touches the #1504 ls face as one of four
batch fixes. This PR is the earlier (2026-11-02) and more faithful
implementation of that face; my recommendation, recorded on #1534 as
well, is to take this PR for the ls face and have #1534 rebase onto
it dropping its ls hunk. The merge itself is left to the repo owner
as usual.

@caomengxuan666

Copy link
Copy Markdown
Member Author

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.

@caomengxuan666

Copy link
Copy Markdown
Member Author

Superseded by the #1534 merge — see the evidence comment just posted for the full record.

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.

ls: COLUMNS env var ignored when stdout is not a terminal (GNU uses it as the width fallback)

1 participant