1 /*
2 * iscygpty.c -- part of ptycheck
3 * https://github.com/k-takata/ptycheck
4 *
5 * Copyright (c) 2015-2017 K.Takata
6 *
7 * You can redistribute it and/or modify it under the terms of either
8 * the MIT license (as described below) or the Vim license.
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * a copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sublicense, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30 #ifdef _WIN32
31
32 #include <ctype.h>
33 #include <io.h>
34 #include <wchar.h>
35 #include <windows.h>
36
37 #ifdef USE_FILEEXTD
38 // VC 7.1 or earlier doesn't support SAL.
39 # if !defined(_MSC_VER) || (_MSC_VER < 1400)
40 # define __out
41 # define __in
42 # define __in_opt
43 # endif
44 // Win32 FileID API Library:
45 // http://www.microsoft.com/en-us/download/details.aspx?id=22599
46 // Needed for WinXP.
47 # include <fileextd.h>
48 #else // USE_FILEEXTD
49 // VC 8 or earlier.
50 # if defined(_MSC_VER) && (_MSC_VER < 1500)
51 # ifdef ENABLE_STUB_IMPL
52 # define STUB_IMPL
53 # else
54 # error "Win32 FileID API Library is required for VC2005 or earlier."
55 # endif
56 # endif
57 #endif // USE_FILEEXTD
58
59
60 #include "iscygpty.h"
61
62 //#define USE_DYNFILEID
63 #ifdef USE_DYNFILEID
64 typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
65 HANDLE hFile,
66 FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
67 LPVOID lpFileInformation,
68 DWORD dwBufferSize);
69 static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
70
71 # ifndef USE_FILEEXTD
stub_GetFileInformationByHandleEx(HANDLE hFile,FILE_INFO_BY_HANDLE_CLASS FileInformationClass,LPVOID lpFileInformation,DWORD dwBufferSize)72 static BOOL WINAPI stub_GetFileInformationByHandleEx(
73 HANDLE hFile,
74 FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
75 LPVOID lpFileInformation,
76 DWORD dwBufferSize)
77 {
78 return FALSE;
79 }
80 # endif
81
setup_fileid_api(void)82 static void setup_fileid_api(void)
83 {
84 if (pGetFileInformationByHandleEx != NULL) {
85 return;
86 }
87 pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx)
88 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
89 "GetFileInformationByHandleEx");
90 if (pGetFileInformationByHandleEx == NULL) {
91 # ifdef USE_FILEEXTD
92 pGetFileInformationByHandleEx = GetFileInformationByHandleEx;
93 # else
94 pGetFileInformationByHandleEx = stub_GetFileInformationByHandleEx;
95 # endif
96 }
97 }
98 #else
99 # define pGetFileInformationByHandleEx GetFileInformationByHandleEx
100 # define setup_fileid_api()
101 #endif
102
103
104 #define is_wprefix(s, prefix) \
105 (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)
106
107 // Check if the fd is a cygwin/msys's pty.
is_cygpty(int fd)108 int is_cygpty(int fd)
109 {
110 #ifdef STUB_IMPL
111 return 0;
112 #else
113 HANDLE h;
114 int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * (MAX_PATH - 1);
115 FILE_NAME_INFO *nameinfo;
116 WCHAR *p = NULL;
117
118 setup_fileid_api();
119
120 h = (HANDLE) _get_osfhandle(fd);
121 if (h == INVALID_HANDLE_VALUE) {
122 return 0;
123 }
124 // Cygwin/msys's pty is a pipe.
125 if (GetFileType(h) != FILE_TYPE_PIPE) {
126 return 0;
127 }
128 nameinfo = malloc(size + sizeof(WCHAR));
129 if (nameinfo == NULL) {
130 return 0;
131 }
132 // Check the name of the pipe:
133 // '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master'
134 if (pGetFileInformationByHandleEx(h, FileNameInfo, nameinfo, size)) {
135 nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0';
136 p = nameinfo->FileName;
137 if (is_wprefix(p, L"\\cygwin-")) { // Cygwin
138 p += 8;
139 } else if (is_wprefix(p, L"\\msys-")) { // MSYS and MSYS2
140 p += 6;
141 } else {
142 p = NULL;
143 }
144 if (p != NULL) {
145 while (*p && isxdigit(*p)) // Skip 16-digit hexadecimal.
146 ++p;
147 if (is_wprefix(p, L"-pty")) {
148 p += 4;
149 } else {
150 p = NULL;
151 }
152 }
153 if (p != NULL) {
154 while (*p && isdigit(*p)) // Skip pty number.
155 ++p;
156 if (is_wprefix(p, L"-from-master")) {
157 //p += 12;
158 } else if (is_wprefix(p, L"-to-master")) {
159 //p += 10;
160 } else {
161 p = NULL;
162 }
163 }
164 }
165 free(nameinfo);
166 return (p != NULL);
167 #endif // STUB_IMPL
168 }
169
170 // Check if at least one cygwin/msys pty is used.
is_cygpty_used(void)171 int is_cygpty_used(void)
172 {
173 int fd, ret = 0;
174
175 for (fd = 0; fd < 3; fd++) {
176 ret |= is_cygpty(fd);
177 }
178 return ret;
179 }
180
181 #endif // _WIN32
182
183 // vim: set ts=4 sw=4:
184