From 71a6ae14434ac358da3384a32f08e842f5eba803 Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Sun, 20 Mar 2022 17:57:00 +0530 Subject: [PATCH 1/2] Added support to read a filename redirected to stdin - Canonicalization of `/dev/stdin` which points to stdin file --- src/uu/stat/src/stat.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 569c94d96..512eb6a8a 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -471,11 +471,18 @@ impl Stater { } fn new(matches: &ArgMatches) -> UResult { - let files: Vec = matches + let mut files: Vec = matches .values_of(ARG_FILES) .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); - + #[cfg(unix)] + if files.contains(&String::from("-")) { + files = Vec::from([Path::new("/dev/stdin") + .canonicalize()? + .into_os_string() + .into_string() + .unwrap()]); + } let format_str = if matches.is_present(options::PRINTF) { matches .value_of(options::PRINTF) From 193899f09ce7922fcbd8f65b1b96e72362c8f621 Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Wed, 23 Mar 2022 20:05:20 +0530 Subject: [PATCH 2/2] Modified code to replace all instances of `-` Now all instances of `-` will be replaced with real / canonicalized path of `/dev/stdin` --- src/uu/stat/src/stat.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 512eb6a8a..3196c373d 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -477,11 +477,17 @@ impl Stater { .unwrap_or_default(); #[cfg(unix)] if files.contains(&String::from("-")) { - files = Vec::from([Path::new("/dev/stdin") - .canonicalize()? + let redirected_path = Path::new("/dev/stdin") + .canonicalize() + .expect("unable to canonicalize /dev/stdin") .into_os_string() .into_string() - .unwrap()]); + .unwrap(); + for file in &mut files { + if file == "-" { + *file = redirected_path.clone(); + } + } } let format_str = if matches.is_present(options::PRINTF) { matches