Skip to content

Commit a247129

Browse files
committed
Add various pty functions
* grantpt * ptsname/ptsname_r * posix_openpt * unlockpt
1 parent 5c90289 commit a247129

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub mod mount;
4444
#[cfg(target_os = "linux")]
4545
pub mod mqueue;
4646

47+
pub mod pty;
48+
4749
#[cfg(any(target_os = "linux", target_os = "macos"))]
4850
pub mod poll;
4951

src/pty.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::ffi::CStr;
2+
use std::os::unix::io::RawFd;
3+
4+
use libc;
5+
6+
use {Errno, Error, fcntl, Result};
7+
8+
pub fn grantpt(fd: RawFd) -> Result<()> {
9+
if unsafe { libc::grantpt(fd) } < 0 {
10+
return Err(Error::last().into());
11+
}
12+
13+
Ok(())
14+
}
15+
16+
pub fn posix_openpt(flags: fcntl::OFlag) -> Result<RawFd> {
17+
let fd = unsafe {
18+
libc::posix_openpt(flags.bits())
19+
};
20+
21+
if fd < 0 {
22+
return Err(Error::last().into());
23+
}
24+
25+
Ok(fd)
26+
}
27+
28+
pub fn ptsname(fd: RawFd) -> Result<String> {
29+
let name_ptr = unsafe { libc::ptsname(fd) };
30+
if name_ptr.is_null() {
31+
return Err(Errno::ENOTTY.into());
32+
}
33+
34+
let name_cstr = unsafe { CStr::from_ptr(name_ptr) };
35+
let name = match name_cstr.to_str() {
36+
Ok(s) => s.to_string(),
37+
Err(_) => {
38+
// FIXME: Unsure what error I should report here.
39+
return Err(Error::last().into());
40+
}
41+
};
42+
Ok(name)
43+
}
44+
45+
#[cfg(any(target_os = "linux",
46+
target_os = "android",
47+
target_os = "emscripten",
48+
target_os = "fuchsia"))]
49+
pub fn ptsname_r(fd: RawFd) -> Result<String> {
50+
let mut name_buf = [0 as libc::c_char; 64];
51+
if unsafe { libc::ptsname_r(fd, name_buf.as_mut_ptr(), name_buf.len()) } != 0 {
52+
return Err(Error::last().into());
53+
}
54+
55+
let name_cstr = unsafe { CStr::from_ptr(name_buf.as_ptr()) };
56+
let name = match name_cstr.to_str() {
57+
Ok(s) => s.to_string(),
58+
Err(_) => {
59+
// FIXME: Unsure what error I should report here.
60+
return Err(Error::last().into());
61+
}
62+
};
63+
Ok(name)
64+
}
65+
66+
pub fn unlockpt(fd: RawFd) -> Result<()> {
67+
if unsafe { libc::unlockpt(fd) } < 0 {
68+
return Err(Error::last().into());
69+
}
70+
71+
Ok(())
72+
}

0 commit comments

Comments
 (0)