diff options
author | Utkarsh Kukreti <utkarshkukreti@gmail.com> | 2015-01-22 16:39:57 +0530 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2015-01-26 10:37:46 -0800 |
commit | cc277646472996833a8670ddb3f927ff409c4084 (patch) | |
tree | 83cb64f3e93035bf6df62c8620eb458ce781289f /src/unistd.rs | |
parent | 819f0e9196d4ea1be81a33ec6d2b5ed8294dfaa0 (diff) | |
download | nix-cc277646472996833a8670ddb3f927ff409c4084.zip |
Add a safe wrapper for `libc::isatty`.
Diffstat (limited to 'src/unistd.rs')
-rw-r--r-- | src/unistd.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/unistd.rs b/src/unistd.rs index 952040fd..579f8de9 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -409,6 +409,21 @@ pub fn ftruncate(fd: Fd, len: off_t) -> SysResult<()> { } } +pub fn isatty(fd: Fd) -> SysResult<bool> { + use {errno, libc}; + + if unsafe { libc::isatty(fd) } == 1 { + Ok(true) + } else { + match SysError::last() { + // ENOTTY means `fd` is a valid file descriptor, but not a TTY, so + // we return `Ok(false)` + SysError { kind: errno::ENOTTY } => Ok(false), + err => Err(err) + } + } +} + #[cfg(target_os = "linux")] mod linux { use std::path::Path; |