Skip to content

Commit 84ad116

Browse files
committed
Use io::stdin() to read from standard input in a portable manner
1 parent 6dc34fe commit 84ad116

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::params::{parse_params, Format};
77
use std::env;
88
use std::ffi::OsString;
99
use std::fs;
10-
use std::io::{self, Write};
10+
use std::io::{self, Read, Write};
1111
use std::process::{exit, ExitCode};
1212

1313
mod context_diff;
@@ -46,8 +46,12 @@ fn main() -> ExitCode {
4646
}
4747
// read files
4848
fn read_file_contents(filepath: &OsString) -> io::Result<Vec<u8>> {
49-
let stdin = OsString::from("/dev/stdin");
50-
fs::read(if filepath == "-" { &stdin } else { filepath })
49+
if filepath == "-" {
50+
let mut content = Vec::new();
51+
io::stdin().read_to_end(&mut content).and(Ok(content))
52+
} else {
53+
fs::read(filepath)
54+
}
5155
}
5256
let from_content = match read_file_contents(&params.from) {
5357
Ok(from_content) => from_content,

0 commit comments

Comments
 (0)