blob: 4a6dc3d42f2ed0face1e6b8217df2ac8ea4234b1 [file] [log] [blame]
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02001/*
2 * iscygpty.c -- part of ptycheck
3 * https://github.com/k-takata/ptycheck
4 *
5 * Copyright (c) 2015-2016 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#include <ctype.h>
31#include <io.h>
32#include <wchar.h>
33#include <windows.h>
34
35#ifdef USE_FILEEXTD
36/* VC 7.1 or earlier doesn't support SAL. */
37# if !defined(_MSC_VER) || (_MSC_VER < 1400)
38# define __out
39# define __in
40# define __in_opt
41# endif
42/* Win32 FileID API Library:
43 * http://www.microsoft.com/en-us/download/details.aspx?id=22599
44 * Needed for WinXP. */
45# include <fileextd.h>
46#else /* USE_FILEEXTD */
47/* VC 8 or earlier. */
48# if defined(_MSC_VER) && (_MSC_VER < 1500)
49# ifdef ENABLE_STUB_IMPL
50# define STUB_IMPL
51# else
52# error "Win32 FileID API Library is required for VC2005 or earlier."
53# endif
54# endif
55#endif /* USE_FILEEXTD */
56
57
58#include "iscygpty.h"
59
60//#define USE_DYNFILEID
61#ifdef USE_DYNFILEID
62typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
63 HANDLE hFile,
64 FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
65 LPVOID lpFileInformation,
66 DWORD dwBufferSize
67);
68static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
69
70# ifndef USE_FILEEXTD
71static BOOL WINAPI stub_GetFileInformationByHandleEx(
72 HANDLE hFile,
73 FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
74 LPVOID lpFileInformation,
75 DWORD dwBufferSize
76 )
77{
78 return FALSE;
79}
80# endif
81
82static 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. */
108int 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;
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);
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. */
171int 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/* vim: set ts=4 sw=4: */