14 fileNameInfo uintptr = 2
19 kernel32 = syscall.NewLazyDLL("kernel32.dll")
20 procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
21 procGetFileInformationByHandleEx = kernel32.NewProc("GetFileInformationByHandleEx")
22 procGetFileType = kernel32.NewProc("GetFileType")
26 // Check if GetFileInformationByHandleEx is available.
27 if procGetFileInformationByHandleEx.Find() != nil {
28 procGetFileInformationByHandleEx = nil
32 // IsTerminal return true if the file descriptor is terminal.
33 func IsTerminal(fd uintptr) bool {
35 r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0)
36 return r != 0 && e == 0
39 // Check pipe name is used for cygwin/msys2 pty.
40 // Cygwin/MSYS2 PTY has a name like:
41 // \{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master
42 func isCygwinPipeName(name string) bool {
43 token := strings.Split(name, "-")
48 if token[0] != `\msys` && token[0] != `\cygwin` {
56 if !strings.HasPrefix(token[2], "pty") {
60 if token[3] != `from` && token[3] != `to` {
64 if token[4] != "master" {
71 // IsCygwinTerminal() return true if the file descriptor is a cygwin or msys2
73 func IsCygwinTerminal(fd uintptr) bool {
74 if procGetFileInformationByHandleEx == nil {
78 // Cygwin/msys's pty is a pipe.
79 ft, _, e := syscall.Syscall(procGetFileType.Addr(), 1, fd, 0, 0)
80 if ft != fileTypePipe || e != 0 {
84 var buf [2 + syscall.MAX_PATH]uint16
85 r, _, e := syscall.Syscall6(procGetFileInformationByHandleEx.Addr(),
86 4, fd, fileNameInfo, uintptr(unsafe.Pointer(&buf)),
87 uintptr(len(buf)*2), 0, 0)
92 l := *(*uint32)(unsafe.Pointer(&buf))
93 return isCygwinPipeName(string(utf16.Decode(buf[2 : 2+l/2])))