-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* grantpt * ptsname/ptsname_r * posix_openpt * unlockpt
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::ffi::CStr; | ||
use std::os::unix::io::RawFd; | ||
|
||
use libc; | ||
|
||
use {Errno, Error, fcntl, Result}; | ||
|
||
pub fn grantpt(fd: RawFd) -> Result<()> { | ||
if unsafe { libc::grantpt(fd) } < 0 { | ||
return Err(Error::last().into()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn posix_openpt(flags: fcntl::OFlag) -> Result<RawFd> { | ||
let fd = unsafe { | ||
libc::posix_openpt(flags.bits()) | ||
}; | ||
|
||
if fd < 0 { | ||
return Err(Error::last().into()); | ||
} | ||
|
||
Ok(fd) | ||
} | ||
|
||
pub fn ptsname(fd: RawFd) -> Result<String> { | ||
let name_ptr = unsafe { libc::ptsname(fd) }; | ||
if name_ptr.is_null() { | ||
return Err(Errno::ENOTTY.into()); | ||
} | ||
|
||
let name_cstr = unsafe { CStr::from_ptr(name_ptr) }; | ||
let name = match name_cstr.to_str() { | ||
Ok(s) => s.to_string(), | ||
Err(_) => { | ||
// FIXME: Unsure what error I should report here. | ||
return Err(Error::last().into()); | ||
} | ||
}; | ||
Ok(name) | ||
} | ||
|
||
#[cfg(any(target_os = "linux", | ||
target_os = "android", | ||
target_os = "emscripten", | ||
target_os = "fuchsia"))] | ||
pub fn ptsname_r(fd: RawFd) -> Result<String> { | ||
let mut name_buf = [0 as libc::c_char; 64]; | ||
if unsafe { libc::ptsname_r(fd, name_buf.as_mut_ptr(), name_buf.len()) } != 0 { | ||
return Err(Error::last().into()); | ||
} | ||
|
||
let name_cstr = unsafe { CStr::from_ptr(name_buf.as_ptr()) }; | ||
let name = match name_cstr.to_str() { | ||
Ok(s) => s.to_string(), | ||
Err(_) => { | ||
// FIXME: Unsure what error I should report here. | ||
return Err(Error::last().into()); | ||
} | ||
}; | ||
Ok(name) | ||
} | ||
|
||
pub fn unlockpt(fd: RawFd) -> Result<()> { | ||
if unsafe { libc::unlockpt(fd) } < 0 { | ||
return Err(Error::last().into()); | ||
} | ||
|
||
Ok(()) | ||
} |