Repository navigation
fix(uniq): treat +NUM after -- as a file name (Fixes #1878) - #2008
caomengxuan666 wants to merge 1 commit into
Conversation
GNU uniq.c:585-589 parses the obsolete +NUM and -NUM options only while the token is still an option, i.e. before getopt's "--". Once "--" has ended option parsing, "+N" is an ordinary operand and is opened as a FILE. Oracle (GNU coreutils 9.4, LC_ALL=C, WSL): $ uniq -- +3 file uniq: +3: No such file or directory rc=1 $ uniq -- +3 </dev/null uniq: +3: No such file or directory rc=1 (no stdin fallback) $ uniq +3 file -> skip 3 chars, rc=0 $ uniq -3 file -> skip 3 fields, rc=0 $ uniq +2 -- file -> skip 2 chars, rc=0 $ uniq -- +3 a b uniq: extra operand 'b' rc=1 (checked before open) Before this change WinuxCmd swallowed "+3" as skip-chars in every one of those positions: `uniq -- +3 file` exited 0 (GNU exits 1), and `uniq -- +3 a b` reported "a: No such file" instead of the extra-operand diagnostic, because the extra NUL/positional splice also misaligned the operand indices. The shared parser consumes "--" and returns bare positionals, so the command body cannot tell whether a token was an option or an operand. collect_operands() re-walks ctx.raw_args with the policy the parser used (the value-taking -f/-s/-w options, the POSIXLY_CORRECT stop rule) and reports which operands came after "--"; build_config() then applies the +NUM rule only to option-position tokens. The pre-glob splice now uses the filtered list instead of indexing ctx.positionals, so a consumed +NUM no longer shifts the INPUT/OUTPUT operands. Tests: six issue1878_* unit tests in tests/unit/uniq/uniq_unit_test.cpp cover -- +NUM as a file (missing and existing), no stdin fallback, +/-NUM before "--" unchanged, and the output-operand splice.
复核结论:需要返工(只补两个单测,实现没问题)头 机制核对(已读 parser 源码)
Oracle 实证(pristine coreutils 9.4,
|
Fixes #1878
Problem
After
--, a leading+NUMoperand must be a file name, but WinuxCmd swallowed it as uniq's obsolete+Nskip-chars option:-NUM/+NUMbefore--must keep their skip-fields / skip-chars meaning.Oracle (GNU coreutils 9.4,
LC_ALL=C, via WSL script file)Root cause
uniq.c:585-589parses the obsolete+NUM/-NUMoptions only while the token is still an option — i.e. before getopt's--. The shared option parser consumes--and returns barectx.positionals, so the command body had no way to telluniq +3 filefromuniq -- +3 fileand treated+3as skip-chars in both.A second, related divergence came from the same code: the pre-glob splice re-indexed
ctx.positionals, so after a+NUMwas consumed the INPUT/OUTPUT operands shifted (anduniq -- +3 a breporteda: No such fileinstead of the extra-operand diagnostic).Fix (
src/commands/uniq.cpp, minimal)collect_operands()re-walksctx.raw_argswith the policy the parser used (value-taking-f/-s/-wand their long forms; thePOSIXLY_CORRECTstop rule) and reports each operand with anafter_double_dashflag. Reusingctx.raw_argsis the established pattern for grammar the parsed ctx cannot express (getopt, column, cal, comm).build_config()applies the+NUM→ skip-chars rule only to option-position tokens; after--the operand is kept as a file name.+NUMoptions no longer shift INPUT/OUTPUT.No new user-visible strings (the
uniq: +3: No such file or directorytext already exists), so no i18n baseline change.Tests
Six new unit tests (
issue1878_*) intests/unit/uniq/uniq_unit_test.cpp:issue1878_plus_num_after_double_dash_is_a_filename— rc=1, empty stdout, exact GNU messageissue1878_plus_num_after_double_dash_alone_does_not_read_stdin— operand never falls back to stdinissue1878_plus_num_after_double_dash_reads_the_file— a real+3file is readissue1878_plus_num_before_double_dash_still_skips_chars—+3 fileand+2 -- fileunchangedissue1878_minus_num_still_skips_fields—-3 fileunchangedissue1878_double_dash_splice_keeps_output_operand—+2 -- in outstill writesoutEvidence:
(32 pre-existing + 6 new)
End-to-end probe against the patched
winuxcmd.exeSame script file run on both sides (WSL):
uniq -- +3 fileuniq: +3: No such file or directoryuniq +3 fileaXXaXXuniq -3 fileaXXaXXuniq +2 -- fileaXXaXXuniq -- -3 fileuniq: -3: No such file or directoryuniq -- +3 a buniq: extra operand 'b'uniq -- +3x fileuniq: +3x: No such file or directoryuniq -- +3 < inGate / ctest scope
rust2/gate/run-gate.sh uniq: not applicable — there is norust2/gate/cases/uniq.tsvand no Rustwinux-cmdsport of uniq (rust2/crates/winux-cmds/src/uniq.rsdoes not exist). The runner reportsERROR: 1 requested command(s) had no case table.uniqis still a C++ command, so this fix is in the command body (no dual track added).cargo test -p winux-cmds: not run — no Rust code was changed.winuxcmd-tests.exe(--gtest_filter=uniq.*, 38/38) rather than a full unisolated ctest.Unverified
POSIXLY_CORRECT=1interaction: the scan mirrors the parser's documented stop rule, but the standalonewinuxcmd.exeprobe cannot exercise env-var semantics under WSL, so it is not evidence-backed here.Formatting
scripts/format.pyreports "No files found that need formatting". The bundled clang-format (22.1.3) flags 75 pre-existing violations onorigin/mainforuniq.cppunder its newer style; none of the lines added by this PR are among them.