1b8a62b91Sopenharmony_ci//! Functions which operate on file descriptors which might be terminals. 2b8a62b91Sopenharmony_ci 3b8a62b91Sopenharmony_ciuse crate::backend; 4b8a62b91Sopenharmony_ci#[cfg(any( 5b8a62b91Sopenharmony_ci all(linux_raw, feature = "procfs"), 6b8a62b91Sopenharmony_ci all(libc, not(any(target_os = "fuchsia", target_os = "wasi"))), 7b8a62b91Sopenharmony_ci))] 8b8a62b91Sopenharmony_ciuse crate::io; 9b8a62b91Sopenharmony_ciuse backend::fd::AsFd; 10b8a62b91Sopenharmony_ci#[cfg(any( 11b8a62b91Sopenharmony_ci all(linux_raw, feature = "procfs"), 12b8a62b91Sopenharmony_ci all(libc, not(any(target_os = "fuchsia", target_os = "wasi"))), 13b8a62b91Sopenharmony_ci))] 14b8a62b91Sopenharmony_ciuse { 15b8a62b91Sopenharmony_ci crate::ffi::CString, crate::path::SMALL_PATH_BUFFER_SIZE, alloc::vec::Vec, 16b8a62b91Sopenharmony_ci backend::fd::BorrowedFd, 17b8a62b91Sopenharmony_ci}; 18b8a62b91Sopenharmony_ci 19b8a62b91Sopenharmony_ci/// `isatty(fd)`—Tests whether a file descriptor refers to a terminal. 20b8a62b91Sopenharmony_ci/// 21b8a62b91Sopenharmony_ci/// # References 22b8a62b91Sopenharmony_ci/// - [POSIX] 23b8a62b91Sopenharmony_ci/// - [Linux] 24b8a62b91Sopenharmony_ci/// 25b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/isatty.html 26b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/isatty.3.html 27b8a62b91Sopenharmony_ci#[inline] 28b8a62b91Sopenharmony_cipub fn isatty<Fd: AsFd>(fd: Fd) -> bool { 29b8a62b91Sopenharmony_ci backend::termios::syscalls::isatty(fd.as_fd()) 30b8a62b91Sopenharmony_ci} 31b8a62b91Sopenharmony_ci 32b8a62b91Sopenharmony_ci/// `ttyname_r(fd)` 33b8a62b91Sopenharmony_ci/// 34b8a62b91Sopenharmony_ci/// If `reuse` is non-empty, reuse its buffer to store the result if possible. 35b8a62b91Sopenharmony_ci/// 36b8a62b91Sopenharmony_ci/// # References 37b8a62b91Sopenharmony_ci/// - [POSIX] 38b8a62b91Sopenharmony_ci/// - [Linux] 39b8a62b91Sopenharmony_ci/// 40b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/ttyname.html 41b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/ttyname.3.html 42b8a62b91Sopenharmony_ci#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))] 43b8a62b91Sopenharmony_ci#[cfg(feature = "procfs")] 44b8a62b91Sopenharmony_ci#[cfg_attr(doc_cfg, doc(cfg(feature = "procfs")))] 45b8a62b91Sopenharmony_ci#[inline] 46b8a62b91Sopenharmony_cipub fn ttyname<Fd: AsFd, B: Into<Vec<u8>>>(dirfd: Fd, reuse: B) -> io::Result<CString> { 47b8a62b91Sopenharmony_ci _ttyname(dirfd.as_fd(), reuse.into()) 48b8a62b91Sopenharmony_ci} 49b8a62b91Sopenharmony_ci 50b8a62b91Sopenharmony_ci#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))] 51b8a62b91Sopenharmony_ci#[cfg(feature = "procfs")] 52b8a62b91Sopenharmony_cifn _ttyname(dirfd: BorrowedFd<'_>, mut buffer: Vec<u8>) -> io::Result<CString> { 53b8a62b91Sopenharmony_ci // This code would benefit from having a better way to read into 54b8a62b91Sopenharmony_ci // uninitialized memory, but that requires `unsafe`. 55b8a62b91Sopenharmony_ci buffer.clear(); 56b8a62b91Sopenharmony_ci buffer.reserve(SMALL_PATH_BUFFER_SIZE); 57b8a62b91Sopenharmony_ci buffer.resize(buffer.capacity(), 0_u8); 58b8a62b91Sopenharmony_ci 59b8a62b91Sopenharmony_ci loop { 60b8a62b91Sopenharmony_ci match backend::termios::syscalls::ttyname(dirfd, &mut buffer) { 61b8a62b91Sopenharmony_ci Err(io::Errno::RANGE) => { 62b8a62b91Sopenharmony_ci buffer.reserve(1); // use `Vec` reallocation strategy to grow capacity exponentially 63b8a62b91Sopenharmony_ci buffer.resize(buffer.capacity(), 0_u8); 64b8a62b91Sopenharmony_ci } 65b8a62b91Sopenharmony_ci Ok(len) => { 66b8a62b91Sopenharmony_ci buffer.resize(len, 0_u8); 67b8a62b91Sopenharmony_ci return Ok(CString::new(buffer).unwrap()); 68b8a62b91Sopenharmony_ci } 69b8a62b91Sopenharmony_ci Err(errno) => return Err(errno), 70b8a62b91Sopenharmony_ci } 71b8a62b91Sopenharmony_ci } 72b8a62b91Sopenharmony_ci} 73