1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #736 from jbcrail/refactor-interactive

Refactor check for standard stream interactivity.
This commit is contained in:
Heather 2015-11-30 08:39:18 +04:00
commit 614044cff0
5 changed files with 45 additions and 23 deletions

View file

@ -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<String>, number: NumberingMode, show_nonprint: bool,
fn open(path: &str) -> Option<(Box<Read>, bool)> {
if path == "-" {
let stdin = stdin();
let interactive = unsafe { isatty(STDIN_FILENO) } != 0 as c_int;
return Some((Box::new(stdin) as Box<Read>, interactive));
return Some((Box::new(stdin) as Box<Read>, is_stdin_interactive()));
}
match File::open(path) {

View file

@ -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<String>) -> 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())
}

View file

@ -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<S, T: Iterator<Item=S>>(iter: T) where S: std::fmt::Display {
fn open<'a>(path: &str) -> Option<(Box<Read + 'a>, bool)> {
if path == "-" {
let stdin = stdin();
let interactive = unsafe { isatty(STDIN_FILENO) } != 0 as c_int;
return Some((Box::new(stdin) as Box<Read>, interactive));
return Some((Box::new(stdin) as Box<Read>, is_stdin_interactive()));
}
match File::open(Path::new(path)) {

View file

@ -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<String>) -> 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
};
}

View file

@ -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<P: AsRef<Path>>(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
}