Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix msys detection with new winapi. #28

Merged
merged 1 commit into from
Jul 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ extern crate termion;

#[cfg(windows)]
use winapi::shared::minwindef::DWORD;
#[cfg(windows)]
use winapi::shared::ntdef::WCHAR;

/// possible stream sources
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -102,9 +104,7 @@ unsafe fn console_on_any(fds: &[DWORD]) -> bool {
/// Returns true if there is an MSYS tty on the given handle.
#[cfg(windows)]
unsafe fn msys_tty_on(fd: DWORD) -> bool {
use std::ffi::OsString;
use std::mem;
use std::os::windows::ffi::OsStringExt;
use std::slice;

use winapi::ctypes::c_void;
Expand All @@ -115,7 +115,7 @@ unsafe fn msys_tty_on(fd: DWORD) -> bool {
use winapi::shared::minwindef::MAX_PATH;

let size = mem::size_of::<FILE_NAME_INFO>();
let mut name_info_bytes = vec![0u8; size + MAX_PATH];
let mut name_info_bytes = vec![0u8; size + MAX_PATH * mem::size_of::<WCHAR>()];
let res = GetFileInformationByHandleEx(
GetStdHandle(fd),
FileNameInfo,
Expand All @@ -125,18 +125,12 @@ unsafe fn msys_tty_on(fd: DWORD) -> bool {
if res == 0 {
return false;
}
let name_info: FILE_NAME_INFO = *(name_info_bytes[0..size].as_ptr() as
*const FILE_NAME_INFO);
let name_bytes =
&name_info_bytes[size..size + name_info.FileNameLength as usize];
let name_u16 = slice::from_raw_parts(
name_bytes.as_ptr() as *const u16,
name_bytes.len() / 2,
let name_info: &FILE_NAME_INFO = &*(name_info_bytes.as_ptr() as *const FILE_NAME_INFO);
let s = slice::from_raw_parts(
name_info.FileName.as_ptr(),
name_info.FileNameLength as usize / 2,
);
let name = OsString::from_wide(name_u16)
.as_os_str()
.to_string_lossy()
.into_owned();
let name = String::from_utf16_lossy(s);
// This checks whether 'pty' exists in the file name, which indicates that
// a pseudo-terminal is attached. To mitigate against false positives
// (e.g., an actual file name that contains 'pty'), we also require that
Expand Down