aboutsummaryrefslogtreecommitdiffstats
path: root/src/pty/std.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-02-23 00:17:01 -0500
committerJesse Luehrs <doy@tozt.net>2021-02-23 00:17:01 -0500
commitf3e8046eff473aa9bf940b7fbd156cf3dfbfa352 (patch)
treea6b3ed53e89556cd801a49a0619ba37e4c397be6 /src/pty/std.rs
parentacb3681ee251599af194c8555344edc65750ef14 (diff)
downloadpty-process-f3e8046eff473aa9bf940b7fbd156cf3dfbfa352.tar.gz
pty-process-f3e8046eff473aa9bf940b7fbd156cf3dfbfa352.zip
also create separate pty structs per backend
for now, they are identical, but i will change them to be specialized next
Diffstat (limited to 'src/pty/std.rs')
-rw-r--r--src/pty/std.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/pty/std.rs b/src/pty/std.rs
new file mode 100644
index 0000000..84097f9
--- /dev/null
+++ b/src/pty/std.rs
@@ -0,0 +1,36 @@
+use crate::error::*;
+
+use std::os::unix::io::FromRawFd as _;
+
+pub struct Pty {
+ pt: std::fs::File,
+ ptsname: std::path::PathBuf,
+}
+
+impl super::Pty for Pty {
+ fn new() -> Result<Self> {
+ let (pt_fd, ptsname) = super::create_pt()?;
+
+ // safe because posix_openpt (or the previous functions operating on
+ // the result) would have returned an Err (causing us to return early)
+ // if the file descriptor was invalid. additionally, into_raw_fd gives
+ // up ownership over the file descriptor, allowing the newly created
+ // File object to take full ownership.
+ let pt = unsafe { std::fs::File::from_raw_fd(pt_fd) };
+
+ Ok(Self { pt, ptsname })
+ }
+
+ fn pt(&self) -> &std::fs::File {
+ &self.pt
+ }
+
+ fn pts(&self) -> Result<std::fs::File> {
+ let fh = std::fs::OpenOptions::new()
+ .read(true)
+ .write(true)
+ .open(&self.ptsname)
+ .map_err(|e| Error::OpenPts(self.ptsname.clone(), e))?;
+ Ok(fh)
+ }
+}