From 84ad116845db263cfb04234fb509011f59cbb8f1 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Thu, 4 Apr 2024 22:14:03 +0200 Subject: [PATCH] Use io::stdin() to read from standard input in a portable manner --- src/main.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index de4bbbb..6858f5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use crate::params::{parse_params, Format}; use std::env; use std::ffi::OsString; use std::fs; -use std::io::{self, Write}; +use std::io::{self, Read, Write}; use std::process::{exit, ExitCode}; mod context_diff; @@ -46,8 +46,12 @@ fn main() -> ExitCode { } // read files fn read_file_contents(filepath: &OsString) -> io::Result> { - let stdin = OsString::from("/dev/stdin"); - fs::read(if filepath == "-" { &stdin } else { filepath }) + if filepath == "-" { + let mut content = Vec::new(); + io::stdin().read_to_end(&mut content).and(Ok(content)) + } else { + fs::read(filepath) + } } let from_content = match read_file_contents(¶ms.from) { Ok(from_content) => from_content,