diff --git a/Cargo.lock b/Cargo.lock index 6448e580a3..d586925af4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3888,6 +3888,7 @@ dependencies = [ "fluent", "half", "libc", + "rustix", "uucore", ] diff --git a/src/uu/od/Cargo.toml b/src/uu/od/Cargo.toml index ecca7fed9c..87579a341c 100644 --- a/src/uu/od/Cargo.toml +++ b/src/uu/od/Cargo.toml @@ -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 diff --git a/src/uu/od/src/multifile_reader.rs b/src/uu/od/src/multifile_reader.rs index 4213fde90e..c924b71469 100644 --- a/src/uu/od/src/multifile_reader.rs +++ b/src/uu/od/src/multifile_reader.rs @@ -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 { + 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;