@@ -120,6 +120,11 @@ unsafe fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
120
120
}
121
121
122
122
unsafe 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
+
123
128
const SIZE : usize = size_of :: < c:: FILE_NAME_INFO > ( ) + c:: MAX_PATH * size_of :: < c:: WCHAR > ( ) ;
124
129
let mut name_info_bytes = Align8 ( [ 0u8 ; SIZE ] ) ;
125
130
let res = c:: GetFileInformationByHandleEx (
@@ -137,11 +142,13 @@ unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
137
142
let name_ptr = name_info_bytes. 0 . as_ptr ( ) . offset ( size_of :: < c:: DWORD > ( ) as isize ) . cast :: < u16 > ( ) ;
138
143
let s = core:: slice:: from_raw_parts ( name_ptr, name_len) ;
139
144
let name = String :: from_utf16_lossy ( s) ;
145
+ // Get the file name only.
146
+ let name = name. rsplit ( '\\' ) . next ( ) . unwrap_or ( & name) ;
140
147
// This checks whether 'pty' exists in the file name, which indicates that
141
148
// a pseudo-terminal is attached. To mitigate against false positives
142
149
// (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-" ) ;
145
152
let is_pty = name. contains ( "-pty" ) ;
146
153
is_msys && is_pty
147
154
}
0 commit comments