ioctl_rs/
lib.rs

1extern crate libc;
2
3use std::io;
4use std::mem;
5use std::os::unix::io::RawFd;
6
7use libc::c_int;
8
9#[cfg(any(target_os = "linux", target_os = "android"))]
10pub use os::linux::*;
11
12#[cfg(target_os = "macos")]
13pub use os::macos::*;
14
15#[cfg(target_os = "freebsd")]
16pub use os::freebsd::*;
17
18#[cfg(target_os = "dragonfly")]
19pub use os::dragonfly::*;
20
21#[cfg(target_os = "openbsd")]
22pub use os::openbsd::*;
23
24mod os;
25
26
27/// Put the terminal in exclusive mode.
28pub fn tiocexcl(fd: RawFd) -> io::Result<()> {
29    match unsafe { ioctl(fd, TIOCEXCL) } {
30        0 => Ok(()),
31        _ => Err(io::Error::last_os_error())
32    }
33}
34
35/// Disable exclusive mode.
36pub fn tiocnxcl(fd: RawFd) -> io::Result<()> {
37    match unsafe { ioctl(fd, TIOCNXCL) } {
38        0 => Ok(()),
39        _ => Err(io::Error::last_os_error())
40    }
41}
42
43/// Get the status of modem bits.
44pub fn tiocmget(fd: RawFd) -> io::Result<c_int> {
45    let mut bits: c_int = unsafe { mem::uninitialized() };
46
47    match unsafe { ioctl(fd, TIOCMGET, &mut bits) } {
48        0 => Ok(bits),
49        _ => Err(io::Error::last_os_error())
50    }
51}
52
53/// Set the status of modem bits.
54pub fn tiocmset(fd: RawFd, bits: c_int) -> io::Result<()> {
55    match unsafe { ioctl(fd, TIOCMSET, &bits) } {
56        0 => Ok(()),
57        _ => Err(io::Error::last_os_error())
58    }
59}
60
61/// Set the indicated modem bits.
62pub fn tiocmbis(fd: RawFd, bits: c_int) -> io::Result<()> {
63    match unsafe { ioctl(fd, TIOCMBIS, &bits) } {
64        0 => Ok(()),
65        _ => Err(io::Error::last_os_error())
66    }
67}
68
69/// Clear the indicated modem bits.
70pub fn tiocmbic(fd: RawFd, bits: c_int) -> io::Result<()> {
71    match unsafe { ioctl(fd, TIOCMBIC, &bits) } {
72        0 => Ok(()),
73        _ => Err(io::Error::last_os_error())
74    }
75}