rustix/termios/
tty.rs

1//! Functions which operate on file descriptors which might be terminals.
2
3use crate::backend;
4#[cfg(feature = "alloc")]
5#[cfg(feature = "fs")]
6#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
7use crate::path::SMALL_PATH_BUFFER_SIZE;
8use backend::fd::AsFd;
9#[cfg(feature = "alloc")]
10#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
11use {crate::ffi::CString, crate::io, alloc::vec::Vec, backend::fd::BorrowedFd};
12
13/// `isatty(fd)`—Tests whether a file descriptor refers to a terminal.
14///
15/// # References
16///  - [POSIX]
17///  - [Linux]
18///
19/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/isatty.html
20/// [Linux]: https://man7.org/linux/man-pages/man3/isatty.3.html
21#[inline]
22pub fn isatty<Fd: AsFd>(fd: Fd) -> bool {
23    backend::termios::syscalls::isatty(fd.as_fd())
24}
25
26/// `ttyname_r(fd)`—Returns the name of the tty open on `fd`.
27///
28/// If `reuse` already has available capacity, reuse it if possible.
29///
30/// On Linux, this function depends on procfs being mounted on /proc.
31///
32/// # References
33///  - [POSIX]
34///  - [Linux]
35///
36/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/ttyname.html
37/// [Linux]: https://man7.org/linux/man-pages/man3/ttyname.3.html
38#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
39#[cfg(feature = "alloc")]
40#[cfg(feature = "fs")]
41#[doc(alias = "ttyname_r")]
42#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
43#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
44#[inline]
45pub fn ttyname<Fd: AsFd, B: Into<Vec<u8>>>(fd: Fd, reuse: B) -> io::Result<CString> {
46    _ttyname(fd.as_fd(), reuse.into())
47}
48
49#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
50#[cfg(feature = "alloc")]
51#[cfg(feature = "fs")]
52#[allow(unsafe_code)]
53fn _ttyname(fd: BorrowedFd<'_>, mut buffer: Vec<u8>) -> io::Result<CString> {
54    buffer.clear();
55    buffer.reserve(SMALL_PATH_BUFFER_SIZE);
56
57    loop {
58        match backend::termios::syscalls::ttyname(fd, buffer.spare_capacity_mut()) {
59            Err(io::Errno::RANGE) => {
60                // Use `Vec` reallocation strategy to grow capacity
61                // exponentially.
62                buffer.reserve(buffer.capacity() + 1);
63            }
64            Ok(len) => {
65                // SAFETY: Assume the backend returns the length of the string
66                // excluding the NUL.
67                unsafe {
68                    buffer.set_len(len + 1);
69                }
70
71                // SAFETY:
72                // - “ttyname_r stores this pathname in the buffer buf”
73                // - [POSIX definition 3.271: Pathname]: “A string that is
74                //   used to identify a file.”
75                // - [POSIX definition 3.375: String]: “A contiguous sequence
76                //   of bytes terminated by and including the first null byte.”
77                //
78                // Thus, there will be a single NUL byte at the end of the
79                // string.
80                //
81                // [POSIX definition 3.271: Pathname]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_271
82                // [POSIX definition 3.375: String]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_375
83                unsafe {
84                    return Ok(CString::from_vec_with_nul_unchecked(buffer));
85                }
86            }
87            Err(errno) => return Err(errno),
88        }
89    }
90}