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).
This commit is contained in:
Joseph Crail 2015-11-29 21:03:53 -05:00 committed by Roy Ivy III
parent 14eccb4335
commit 9c4c9f6782

View file

@ -12,6 +12,7 @@
// be backported to stable (<= 1.1). This will likely be dropped // be backported to stable (<= 1.1). This will likely be dropped
// when the path trait stabilizes. // when the path trait stabilizes.
use ::libc;
use std::env; use std::env;
use std::fs; use std::fs;
use std::io::{Error, ErrorKind, Result}; use std::io::{Error, ErrorKind, Result};
@ -142,3 +143,33 @@ pub fn canonicalize<P: AsRef<Path>>(original: P, can_mode: CanonicalizeMode) ->
} }
Ok(result) 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
}