Skip to content

more: copy input unpaged when stdout is not a terminal - #16944

Open
aaron-ang wants to merge 6 commits into
uutils:mainfrom
aaron-ang:more-pipe-output
Open

aaron-ang wants to merge 6 commits into
uutils:mainfrom
aaron-ang:more-pipe-output

Conversation

@aaron-ang

@aaron-ang aaron-ang commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

When stdout isn't a terminal, more still ran the pager. more f > out wrote escape codes and ~ filler lines, more f | wc -l hung waiting for a key, and with no terminal at all it failed with more: Would block.

It now copies the input to stdout the way util-linux does:

  • piped stdin is written first;
  • with more than one input, each file gets the :::::::::::::: header;
  • -F applies to the first input and -P skips to the first match, as when paging; -s and -n are ignored.

Added integration tests for these cases, including -P on a FIFO.

Part of #16940

Copilot AI balanced review requested due to automatic review settings November 28, 2026 17:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Piped stdin comes first, and a header with the file name comes before
each file when there is more than one input. -F and -P still pick the
first line written.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 02:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/uu/more/src/more.rs Outdated
let mut reader = BufReader::new(file);
let copied = skip_to_start(&mut reader, from_line, options.pattern.as_deref())
.and_then(|start| out.write_all(&start))
.and_then(|()| std::io::copy(&mut reader, &mut out));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a write error on stdout (ENOSPC, EPIPE) ends up reported as <file>: ... and we carry on with the next file, no?
please keep the read and write errors apart

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created a helper copy_until_read_error to handle a write error and stop. currently util-linux does not print an error and exits 0 on write error, so our implementation is more complete

Comment thread src/uu/more/src/more.rs Outdated
if let Err(e) = copied {
show!(USimpleError::new(
0,
format!("{}: {}", file_os.maybe_quote(), strip_errno(&e))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a MoreError variant instead of the format!()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, added the enum variant

Comment thread src/uu/more/src/more.rs Outdated
continue;
};
if show_header {
out.write_all(b"::::::::::::::\n")?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same separator as MULTI_FILE_TOP_PROMPT, could be shared?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to a top-level const

Read errors are still reported per input, including piped stdin, and the next input is shown. Add a CannotRead error and share the header rule with the pager.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 15:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

musl says "I/O error" where glibc says "Input/output error", so build
the expected message from the platform's strerror.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 16:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codspeed

codspeed Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 4.57%

1 warning

⚠️ 39 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚡ 1 improved benchmark
✅ 63 untouched benchmarks
⏩ 421 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ df_deep_directory 940.3 µs 899.2 µs +4.57%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing aaron-ang:more-pipe-output (f6ee017) with main (53ca8ad)

Open in CodSpeed

Footnotes

  1. 421 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread src/uu/more/src/more.rs Outdated
0,
translate!(
"more-error-cannot-read",
"path" => "standard input",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use MoreError::CannotRead here too, no?

@aaron-ang aaron-ang Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we could. would need some refactoring to generalize the inputs to the method. i opted for Option<&OsStr> for the enum variant field, where None refers to stdin.

Comment thread src/uu/more/src/more.rs Outdated
if reader.read_until(b'\n', &mut scanned)? == 0 {
return Ok(scanned);
}
if String::from_utf8_lossy(&scanned[line_start..]).contains(pattern) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the pattern is never found, the whole file ends up in memory, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored to only keep at most one line in memory.

Searching for the pattern kept every line read, so a file without a
match ended up in memory. Hold one line at a time and seek back to
where the search started; a FIFO can't seek, so nothing is shown.

Report stdin read errors with MoreError::CannotRead like files.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 19:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

When stdout isn't a terminal, write every input unchanged, the way less
does, so the output can be used as data. The start line and pattern
still apply when paging.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 19:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

…ttern"

This reverts commit 1f1553d. util-linux applies the start line and
pattern to piped output too, and compatibility with it comes first.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 19:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

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.

3 participants