std\sys\io\is_terminal/
windows.rs

1use crate::ffi::c_void;
2use crate::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle};
3use crate::sys::c;
4
5pub fn is_terminal(h: &impl AsHandle) -> bool {
6    handle_is_console(h.as_handle())
7}
8
9fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
10    // A null handle means the process has no console.
11    if handle.as_raw_handle().is_null() {
12        return false;
13    }
14
15    let mut out = 0;
16    if unsafe { c::GetConsoleMode(handle.as_raw_handle(), &mut out) != 0 } {
17        // False positives aren't possible. If we got a console then we definitely have a console.
18        return true;
19    }
20
21    // Otherwise, we fall back to an msys hack to see if we can detect the presence of a pty.
22    msys_tty_on(handle)
23}
24
25fn msys_tty_on(handle: BorrowedHandle<'_>) -> bool {
26    // Early return if the handle is not a pipe.
27    if unsafe { c::GetFileType(handle.as_raw_handle()) != c::FILE_TYPE_PIPE } {
28        return false;
29    }
30
31    /// Mirrors [`FILE_NAME_INFO`], giving it a fixed length that we can stack
32    /// allocate
33    ///
34    /// [`FILE_NAME_INFO`]: https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_name_info
35    #[repr(C)]
36    #[allow(non_snake_case)]
37    struct FILE_NAME_INFO {
38        FileNameLength: u32,
39        FileName: [u16; c::MAX_PATH as usize],
40    }
41    let mut name_info = FILE_NAME_INFO { FileNameLength: 0, FileName: [0; c::MAX_PATH as usize] };
42    // Safety: buffer length is fixed.
43    let res = unsafe {
44        c::GetFileInformationByHandleEx(
45            handle.as_raw_handle(),
46            c::FileNameInfo,
47            (&raw mut name_info) as *mut c_void,
48            size_of::<FILE_NAME_INFO>() as u32,
49        )
50    };
51    if res == 0 {
52        return false;
53    }
54
55    // Use `get` because `FileNameLength` can be out of range.
56    let s = match name_info.FileName.get(..name_info.FileNameLength as usize / 2) {
57        None => return false,
58        Some(s) => s,
59    };
60    let name = String::from_utf16_lossy(s);
61    // Get the file name only.
62    let name = name.rsplit('\\').next().unwrap_or(&name);
63    // This checks whether 'pty' exists in the file name, which indicates that
64    // a pseudo-terminal is attached. To mitigate against false positives
65    // (e.g., an actual file name that contains 'pty'), we also require that
66    // the file name begins with either the strings 'msys-' or 'cygwin-'.)
67    let is_msys = name.starts_with("msys-") || name.starts_with("cygwin-");
68    let is_pty = name.contains("-pty");
69    is_msys && is_pty
70}