blob: 722f88f2f46dcbb8489fb2600893373c4d49009d [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 *
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +02005 * Copyright (c) 2015-2017 K.Takata
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02006 *
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
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +020030#ifdef _WIN32
31
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020032#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
64typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +020065 HANDLE hFile,
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020066 FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +020067 LPVOID lpFileInformation,
68 DWORD dwBufferSize
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020069);
70static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
71
72# ifndef USE_FILEEXTD
73static BOOL WINAPI stub_GetFileInformationByHandleEx(
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +020074 HANDLE hFile,
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020075 FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +020076 LPVOID lpFileInformation,
77 DWORD dwBufferSize
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020078 )
79{
80 return FALSE;
81}
82# endif
83
84static void setup_fileid_api(void)
85{
86 if (pGetFileInformationByHandleEx != NULL) {
87 return;
88 }
89 pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx)
90 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
91 "GetFileInformationByHandleEx");
92 if (pGetFileInformationByHandleEx == NULL) {
93# ifdef USE_FILEEXTD
94 pGetFileInformationByHandleEx = GetFileInformationByHandleEx;
95# else
96 pGetFileInformationByHandleEx = stub_GetFileInformationByHandleEx;
97# endif
98 }
99}
100#else
101# define pGetFileInformationByHandleEx GetFileInformationByHandleEx
102# define setup_fileid_api()
103#endif
104
105
106#define is_wprefix(s, prefix) \
107 (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)
108
109/* Check if the fd is a cygwin/msys's pty. */
110int is_cygpty(int fd)
111{
112#ifdef STUB_IMPL
113 return 0;
114#else
115 HANDLE h;
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +0200116 int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * (MAX_PATH - 1);
Bram Moolenaar97ff9b92016-06-26 20:37:46 +0200117 FILE_NAME_INFO *nameinfo;
118 WCHAR *p = NULL;
119
120 setup_fileid_api();
121
122 h = (HANDLE) _get_osfhandle(fd);
123 if (h == INVALID_HANDLE_VALUE) {
124 return 0;
125 }
126 /* Cygwin/msys's pty is a pipe. */
127 if (GetFileType(h) != FILE_TYPE_PIPE) {
128 return 0;
129 }
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +0200130 nameinfo = malloc(size + sizeof(WCHAR));
Bram Moolenaar97ff9b92016-06-26 20:37:46 +0200131 if (nameinfo == NULL) {
132 return 0;
133 }
134 /* Check the name of the pipe:
135 * '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master' */
136 if (pGetFileInformationByHandleEx(h, FileNameInfo, nameinfo, size)) {
137 nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0';
138 p = nameinfo->FileName;
139 if (is_wprefix(p, L"\\cygwin-")) { /* Cygwin */
140 p += 8;
141 } else if (is_wprefix(p, L"\\msys-")) { /* MSYS and MSYS2 */
142 p += 6;
143 } else {
144 p = NULL;
145 }
146 if (p != NULL) {
147 while (*p && isxdigit(*p)) /* Skip 16-digit hexadecimal. */
148 ++p;
149 if (is_wprefix(p, L"-pty")) {
150 p += 4;
151 } else {
152 p = NULL;
153 }
154 }
155 if (p != NULL) {
156 while (*p && isdigit(*p)) /* Skip pty number. */
157 ++p;
158 if (is_wprefix(p, L"-from-master")) {
159 //p += 12;
160 } else if (is_wprefix(p, L"-to-master")) {
161 //p += 10;
162 } else {
163 p = NULL;
164 }
165 }
166 }
167 free(nameinfo);
168 return (p != NULL);
169#endif /* STUB_IMPL */
170}
171
172/* Check if at least one cygwin/msys pty is used. */
173int is_cygpty_used(void)
174{
175 int fd, ret = 0;
176
177 for (fd = 0; fd < 3; fd++) {
178 ret |= is_cygpty(fd);
179 }
180 return ret;
181}
182
Bram Moolenaar9e8dcf92017-08-31 21:35:45 +0200183#endif /* _WIN32 */
184
185/* vim: set ts=4 sw=4: */