od: remove unsafe, GNU stdin compat on wasi (#12205)

This commit is contained in:
oech3
2026-05-10 14:47:42 +09:00
committed by GitHub
parent f65350a3ad
commit 50ef31aa42
3 changed files with 13 additions and 8 deletions
Generated
+1
View File
@@ -3888,6 +3888,7 @@ dependencies = [
"fluent",
"half",
"libc",
"rustix",
"uucore",
]
+1
View File
@@ -21,6 +21,7 @@ path = "src/od.rs"
byteorder = { workspace = true }
clap = { workspace = true }
half = { workspace = true }
rustix = { workspace = true, features = ["stdio"] }
uucore = { workspace = true, features = ["parser-size"] }
fluent = { workspace = true }
libc.workspace = true
+11 -8
View File
@@ -6,8 +6,6 @@
use std::fs::File;
use std::io;
#[cfg(unix)]
use std::os::fd::{AsRawFd, FromRawFd};
use uucore::display::Quotable;
use uucore::show_error;
@@ -56,20 +54,25 @@ impl MultifileReader<'_> {
// For performance reasons we do still do buffered reads from stdin, but
// the buffering is done elsewhere and in a way that is aware of the `-N`
// limit.
let stdin = io::stdin();
#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
{
let stdin_raw_fd = stdin.as_raw_fd();
let stdin_file = unsafe { File::from_raw_fd(stdin_raw_fd) };
self.curr_file = Some(Box::new(stdin_file));
struct RawReader<'a>(rustix::fd::BorrowedFd<'a>);
impl io::Read for RawReader<'_> {
fn read(&mut self, b: &mut [u8]) -> io::Result<usize> {
rustix::io::read(self.0, b).map_err(Into::into)
}
}
let stdin = RawReader(rustix::stdio::stdin());
self.curr_file = Some(Box::new(stdin));
}
// For non-unix platforms we don't have GNU compatibility requirements, so
// we don't need to prevent stdin buffering. This is sub-optimal (since
// there will still be additional buffering further up the stack), but
// doesn't seem worth worrying about at this time.
#[cfg(not(unix))]
#[cfg(not(any(unix, target_os = "wasi")))]
{
let stdin = io::stdin();
self.curr_file = Some(Box::new(stdin));
}
break;