@@ -120,6 +120,11 @@ unsafe fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
120120}
121121
122122unsafe fn msys_tty_on ( handle : c:: HANDLE ) -> bool {
123+ // Early return if the handle is not a pipe.
124+ if c:: GetFileType ( handle) != c:: FILE_TYPE_PIPE {
125+ return false ;
126+ }
127+
123128 const SIZE : usize = size_of :: < c:: FILE_NAME_INFO > ( ) + c:: MAX_PATH * size_of :: < c:: WCHAR > ( ) ;
124129 let mut name_info_bytes = Align8 ( [ 0u8 ; SIZE ] ) ;
125130 let res = c:: GetFileInformationByHandleEx (
@@ -137,11 +142,13 @@ unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
137142 let name_ptr = name_info_bytes. 0 . as_ptr ( ) . offset ( size_of :: < c:: DWORD > ( ) as isize ) . cast :: < u16 > ( ) ;
138143 let s = core:: slice:: from_raw_parts ( name_ptr, name_len) ;
139144 let name = String :: from_utf16_lossy ( s) ;
145+ // Get the file name only.
146+ let name = name. rsplit ( '\\' ) . next ( ) . unwrap_or ( & name) ;
140147 // This checks whether 'pty' exists in the file name, which indicates that
141148 // a pseudo-terminal is attached. To mitigate against false positives
142149 // (e.g., an actual file name that contains 'pty'), we also require that
143- // either the strings 'msys-' or 'cygwin-' are in the file name as well .)
144- let is_msys = name. contains ( "msys-" ) || name. contains ( "cygwin-" ) ;
150+ // the file name begins with either the strings 'msys-' or 'cygwin-'.)
151+ let is_msys = name. starts_with ( "msys-" ) || name. starts_with ( "cygwin-" ) ;
145152 let is_pty = name. contains ( "-pty" ) ;
146153 is_msys && is_pty
147154}
0 commit comments