diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-06-16 00:44:59 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-07-03 16:47:13 -0400 |
commit | b5eef58e549c34f543316d3c1fd032f8475d42df (patch) | |
tree | a9cbca8fc3bda1da1c8b47469de37a28b4e3a63a /src/unistd.rs | |
parent | 6f0ca030a30fd813ad74f7dc4d63d3d5538090b0 (diff) | |
download | nix-b5eef58e549c34f543316d3c1fd032f8475d42df.zip |
implement ttyname
Diffstat (limited to 'src/unistd.rs')
-rw-r--r-- | src/unistd.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/unistd.rs b/src/unistd.rs index f2902bb4..d53b438c 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -2774,3 +2774,20 @@ impl Group { }) } } + +/// Get the name of the terminal device that is open on file descriptor fd +/// (see [`ttyname(3)`](http://man7.org/linux/man-pages/man3/ttyname.3.html)). +pub fn ttyname(fd: RawFd) -> Result<PathBuf> { + const PATH_MAX: usize = libc::PATH_MAX as usize; + let mut buf = vec![0_u8; PATH_MAX]; + let c_buf = buf.as_mut_ptr() as *mut libc::c_char; + + let ret = unsafe { libc::ttyname_r(fd, c_buf, buf.len()) }; + if ret != 0 { + return Err(Error::Sys(Errno::from_i32(ret))); + } + + let nul = buf.iter().position(|c| *c == b'\0').unwrap(); + buf.truncate(nul); + Ok(OsString::from_vec(buf).into()) +} |