From 9c4c9f67824caa3ff20b47811ff5ebbab85a39bd Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Sun, 29 Nov 2015 21:03:53 -0500 Subject: [PATCH] Refactor check for standard stream interactivity. Since several utilities check if the standard streams are interactive, I moved this into the uucore::fs library as is_std*_interactive(). I also added Windows support for these methods, which only return false (or at least until someone finds a way to support this). --- src/uucore/fs.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/uucore/fs.rs b/src/uucore/fs.rs index e0372b357..461c70940 100644 --- a/src/uucore/fs.rs +++ b/src/uucore/fs.rs @@ -12,6 +12,7 @@ // be backported to stable (<= 1.1). This will likely be dropped // when the path trait stabilizes. +use ::libc; use std::env; use std::fs; use std::io::{Error, ErrorKind, Result}; @@ -142,3 +143,33 @@ pub fn canonicalize>(original: P, can_mode: CanonicalizeMode) -> } Ok(result) } + +#[cfg(unix)] +pub fn is_stdin_interactive() -> bool { + unsafe { libc::isatty(libc::STDIN_FILENO) == 1 } +} + +#[cfg(windows)] +pub fn is_stdin_interactive() -> bool { + 0 +} + +#[cfg(unix)] +pub fn is_stdout_interactive() -> bool { + unsafe { libc::isatty(libc::STDOUT_FILENO) == 1 } +} + +#[cfg(windows)] +pub fn is_stdout_interactive() -> bool { + 0 +} + +#[cfg(unix)] +pub fn is_stderr_interactive() -> bool { + unsafe { libc::isatty(libc::STDERR_FILENO) == 1 } +} + +#[cfg(windows)] +pub fn is_stderr_interactive() -> bool { + 0 +}