Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public rates #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ keywords = ["termios", "tty", "terminal", "posix"]

[dependencies]
libc = "0.2"
ioctl-rs = "0.1.5"
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
//! ```

extern crate libc;
extern crate ioctl_rs as ioctl;


use std::io;
use std::mem;
Expand Down Expand Up @@ -171,6 +173,7 @@ impl Termios {
/// Creates a `Termios` structure based on the current settings of a file descriptor.
///
/// `fd` must be an open file descriptor for a terminal device.
#[cfg(not(target_os = "linux"))]
pub fn from_fd(fd: RawFd) -> io::Result<Self> {
let mut termios = unsafe { mem::uninitialized() };

Expand All @@ -180,6 +183,25 @@ impl Termios {
}
}

/// Creates a `Termios` structure based on the current settings of a file descriptor.
///
/// `fd` must be an open file descriptor for a terminal device.
#[cfg(target_os = "linux")]
pub fn from_fd(fd: RawFd) -> io::Result<Self> {
let mut termios: Termios = unsafe { mem::uninitialized() };

// on Linux, we use an ioctl to get the correct data structure
let err = unsafe { ioctl::ioctl(fd, os::linux::TCGETS2,
&mut termios.inner) };

if err != 0 {
return Err(io::Error::last_os_error())
}

Ok(termios)
}


fn inner(&self) -> &::os::target::termios {
&self.inner
}
Expand Down
8 changes: 6 additions & 2 deletions src/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub struct termios {
pub c_lflag: tcflag_t,
c_line: cc_t,
pub c_cc: [cc_t; NCCS],
c_ispeed: speed_t,
c_ospeed: speed_t
pub c_ispeed: speed_t,
pub c_ospeed: speed_t,
}

pub const NCCS: usize = 32;
Expand Down Expand Up @@ -178,3 +178,7 @@ pub const TCIOFLUSH: c_int = 2;
pub const TCSANOW: c_int = 0;
pub const TCSADRAIN: c_int = 1;
pub const TCSAFLUSH: c_int = 2;

// ioctls should be a c_uint, not a c_int. the warning cause by this should
// be ignore until the bug in ioctl-rs is fixed.
pub const TCGETS2: c_int = (0x802c_542a as c_int);