diff --git a/src/cat/cat.rs b/src/cat/cat.rs index ad5a6d857..a1b8e79c2 100644 --- a/src/cat/cat.rs +++ b/src/cat/cat.rs @@ -17,12 +17,11 @@ extern crate libc; #[macro_use] extern crate uucore; -use libc::STDIN_FILENO; -use libc::{c_int, isatty}; use getopts::Options; use std::fs::File; use std::intrinsics::{copy_nonoverlapping}; use std::io::{stdout, stdin, stderr, Write, Read, Result}; +use uucore::fs::is_stdin_interactive; static NAME: &'static str = "cat"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); @@ -260,8 +259,7 @@ fn exec(files: Vec, number: NumberingMode, show_nonprint: bool, fn open(path: &str) -> Option<(Box, bool)> { if path == "-" { let stdin = stdin(); - let interactive = unsafe { isatty(STDIN_FILENO) } != 0 as c_int; - return Some((Box::new(stdin) as Box, interactive)); + return Some((Box::new(stdin) as Box, is_stdin_interactive())); } match File::open(path) { diff --git a/src/nohup/nohup.rs b/src/nohup/nohup.rs index 4f47c123b..748000b18 100644 --- a/src/nohup/nohup.rs +++ b/src/nohup/nohup.rs @@ -15,7 +15,7 @@ extern crate libc; #[macro_use] extern crate uucore; -use libc::{c_char, signal, dup2, execvp, isatty}; +use libc::{c_char, signal, dup2, execvp}; use libc::{SIG_IGN, SIGHUP}; use std::ffi::CString; use std::fs::{File, OpenOptions}; @@ -23,6 +23,7 @@ use std::io::{Error, Write}; use std::os::unix::prelude::*; use std::path::{Path, PathBuf}; use std::env; +use uucore::fs::{is_stderr_interactive, is_stdin_interactive, is_stdout_interactive}; static NAME: &'static str = "nohup"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); @@ -71,11 +72,7 @@ pub fn uumain(args: Vec) -> i32 { } fn replace_fds() { - let replace_stdin = unsafe { isatty(libc::STDIN_FILENO) == 1 }; - let replace_stdout = unsafe { isatty(libc::STDOUT_FILENO) == 1 }; - let replace_stderr = unsafe { isatty(libc::STDERR_FILENO) == 1 }; - - if replace_stdin { + if is_stdin_interactive() { let new_stdin = match File::open(Path::new("/dev/null")) { Ok(t) => t, Err(e) => { @@ -87,7 +84,7 @@ fn replace_fds() { } } - if replace_stdout { + if is_stdout_interactive() { let new_stdout = find_stdout(); let fd = new_stdout.as_raw_fd(); @@ -96,7 +93,7 @@ fn replace_fds() { } } - if replace_stderr { + if is_stderr_interactive() { if unsafe { dup2(1, 2) } != 2 { crash!(2, "Cannot replace STDERR: {}", Error::last_os_error()) } diff --git a/src/sort/sort.rs b/src/sort/sort.rs index f0d7ae6e5..2f44ac318 100644 --- a/src/sort/sort.rs +++ b/src/sort/sort.rs @@ -17,12 +17,11 @@ extern crate libc; #[macro_use] extern crate uucore; -use libc::STDIN_FILENO; -use libc::{c_int, isatty}; use std::cmp::Ordering; use std::fs::File; use std::io::{BufRead, BufReader, Read, stdin, Write}; use std::path::Path; +use uucore::fs::is_stdin_interactive; static NAME: &'static str = "sort"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); @@ -194,8 +193,7 @@ fn print_sorted>(iter: T) where S: std::fmt::Display { fn open<'a>(path: &str) -> Option<(Box, bool)> { if path == "-" { let stdin = stdin(); - let interactive = unsafe { isatty(STDIN_FILENO) } != 0 as c_int; - return Some((Box::new(stdin) as Box, interactive)); + return Some((Box::new(stdin) as Box, is_stdin_interactive())); } match File::open(Path::new(path)) { diff --git a/src/tty/tty.rs b/src/tty/tty.rs index 097a77423..b70cdc272 100644 --- a/src/tty/tty.rs +++ b/src/tty/tty.rs @@ -19,10 +19,10 @@ extern crate uucore; use std::ffi::CStr; use std::io::Write; +use uucore::fs::is_stdin_interactive; extern { fn ttyname(filedesc: libc::c_int) -> *const libc::c_char; - fn isatty(filedesc: libc::c_int) -> libc::c_int; } static NAME: &'static str = "tty"; @@ -69,12 +69,10 @@ pub fn uumain(args: Vec) -> i32 { } } - return unsafe { - if isatty(libc::STDIN_FILENO) == 1 { - libc::EXIT_SUCCESS - } else { - libc::EXIT_FAILURE - } + return if is_stdin_interactive() { + libc::EXIT_SUCCESS + } else { + libc::EXIT_FAILURE }; } 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 +}