1*c7ef0cfcSnicm /****************************************************************************
2*c7ef0cfcSnicm * Copyright 2020-2021,2023 Thomas E. Dickey *
3*c7ef0cfcSnicm * Copyright 1998-2009,2010 Free Software Foundation, Inc. *
4*c7ef0cfcSnicm * *
5*c7ef0cfcSnicm * Permission is hereby granted, free of charge, to any person obtaining a *
6*c7ef0cfcSnicm * copy of this software and associated documentation files (the *
7*c7ef0cfcSnicm * "Software"), to deal in the Software without restriction, including *
8*c7ef0cfcSnicm * without limitation the rights to use, copy, modify, merge, publish, *
9*c7ef0cfcSnicm * distribute, distribute with modifications, sublicense, and/or sell *
10*c7ef0cfcSnicm * copies of the Software, and to permit persons to whom the Software is *
11*c7ef0cfcSnicm * furnished to do so, subject to the following conditions: *
12*c7ef0cfcSnicm * *
13*c7ef0cfcSnicm * The above copyright notice and this permission notice shall be included *
14*c7ef0cfcSnicm * in all copies or substantial portions of the Software. *
15*c7ef0cfcSnicm * *
16*c7ef0cfcSnicm * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17*c7ef0cfcSnicm * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18*c7ef0cfcSnicm * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19*c7ef0cfcSnicm * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20*c7ef0cfcSnicm * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21*c7ef0cfcSnicm * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22*c7ef0cfcSnicm * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23*c7ef0cfcSnicm * *
24*c7ef0cfcSnicm * Except as contained in this notice, the name(s) of the above copyright *
25*c7ef0cfcSnicm * holders shall not be used in advertising or otherwise to promote the *
26*c7ef0cfcSnicm * sale, use or other dealings in this Software without prior written *
27*c7ef0cfcSnicm * authorization. *
28*c7ef0cfcSnicm ****************************************************************************/
29*c7ef0cfcSnicm
30*c7ef0cfcSnicm /****************************************************************************
31*c7ef0cfcSnicm * Author: Juergen Pfeifer *
32*c7ef0cfcSnicm * and: Thomas E. Dickey *
33*c7ef0cfcSnicm ****************************************************************************/
34*c7ef0cfcSnicm
35*c7ef0cfcSnicm #include <curses.priv.h>
36*c7ef0cfcSnicm
37*c7ef0cfcSnicm MODULE_ID("$Id: lib_win32util.c,v 1.1 2023/10/17 09:52:09 nicm Exp $")
38*c7ef0cfcSnicm
39*c7ef0cfcSnicm #ifdef _NC_WINDOWS
40*c7ef0cfcSnicm #include <io.h>
41*c7ef0cfcSnicm
42*c7ef0cfcSnicm #ifdef _NC_CHECK_MINTTY
43*c7ef0cfcSnicm #define PSAPI_VERSION 2
44*c7ef0cfcSnicm #include <psapi.h>
45*c7ef0cfcSnicm #include <tchar.h>
46*c7ef0cfcSnicm
47*c7ef0cfcSnicm #define array_length(a) (sizeof(a)/sizeof(a[0]))
48*c7ef0cfcSnicm
49*c7ef0cfcSnicm /* This function tests, whether or not the ncurses application
50*c7ef0cfcSnicm is running as a descendant of MSYS2/cygwin mintty terminal
51*c7ef0cfcSnicm application. mintty doesn't use Windows Console for its screen
52*c7ef0cfcSnicm I/O, so the native Windows _isatty doesn't recognize it as
53*c7ef0cfcSnicm character device. But we can discover we are at the end of an
54*c7ef0cfcSnicm Pipe and can query the server side of the pipe, looking whether
55*c7ef0cfcSnicm or not this is mintty.
56*c7ef0cfcSnicm For now we terminate the program if we discover that situation.
57*c7ef0cfcSnicm Although in theory it would be possible, to remotely manipulate
58*c7ef0cfcSnicm the terminal state of mintty, this is out of scope for now and
59*c7ef0cfcSnicm not worth the significant effort.
60*c7ef0cfcSnicm */
NCURSES_EXPORT(int)61*c7ef0cfcSnicm NCURSES_EXPORT(int)
62*c7ef0cfcSnicm _nc_console_checkmintty(int fd, LPHANDLE pMinTTY)
63*c7ef0cfcSnicm {
64*c7ef0cfcSnicm HANDLE handle = _nc_console_handle(fd);
65*c7ef0cfcSnicm DWORD dw;
66*c7ef0cfcSnicm int code = 0;
67*c7ef0cfcSnicm
68*c7ef0cfcSnicm T((T_CALLED("lib_winhelper::_nc_console_checkmintty(%d, %p)"), fd, pMinTTY));
69*c7ef0cfcSnicm
70*c7ef0cfcSnicm if (handle != INVALID_HANDLE_VALUE) {
71*c7ef0cfcSnicm dw = GetFileType(handle);
72*c7ef0cfcSnicm if (dw == FILE_TYPE_PIPE) {
73*c7ef0cfcSnicm if (GetNamedPipeInfo(handle, 0, 0, 0, 0)) {
74*c7ef0cfcSnicm ULONG pPid;
75*c7ef0cfcSnicm /* Requires NT6 */
76*c7ef0cfcSnicm if (GetNamedPipeServerProcessId(handle, &pPid)) {
77*c7ef0cfcSnicm TCHAR buf[MAX_PATH];
78*c7ef0cfcSnicm DWORD len = 0;
79*c7ef0cfcSnicm /* These security attributes may allow us to
80*c7ef0cfcSnicm create a remote thread in mintty to manipulate
81*c7ef0cfcSnicm the terminal state remotely */
82*c7ef0cfcSnicm HANDLE pHandle = OpenProcess(PROCESS_CREATE_THREAD
83*c7ef0cfcSnicm | PROCESS_QUERY_INFORMATION
84*c7ef0cfcSnicm | PROCESS_VM_OPERATION
85*c7ef0cfcSnicm | PROCESS_VM_WRITE
86*c7ef0cfcSnicm | PROCESS_VM_READ,
87*c7ef0cfcSnicm FALSE,
88*c7ef0cfcSnicm pPid);
89*c7ef0cfcSnicm if (pMinTTY)
90*c7ef0cfcSnicm *pMinTTY = INVALID_HANDLE_VALUE;
91*c7ef0cfcSnicm if (pHandle != INVALID_HANDLE_VALUE) {
92*c7ef0cfcSnicm if ((len = GetProcessImageFileName(pHandle,
93*c7ef0cfcSnicm buf,
94*c7ef0cfcSnicm (DWORD)
95*c7ef0cfcSnicm array_length(buf)))) {
96*c7ef0cfcSnicm TCHAR *pos = _tcsrchr(buf, _T('\\'));
97*c7ef0cfcSnicm if (pos) {
98*c7ef0cfcSnicm pos++;
99*c7ef0cfcSnicm if (_tcsnicmp(pos, _TEXT("mintty.exe"), 10)
100*c7ef0cfcSnicm == 0) {
101*c7ef0cfcSnicm if (pMinTTY)
102*c7ef0cfcSnicm *pMinTTY = pHandle;
103*c7ef0cfcSnicm code = 1;
104*c7ef0cfcSnicm }
105*c7ef0cfcSnicm }
106*c7ef0cfcSnicm }
107*c7ef0cfcSnicm }
108*c7ef0cfcSnicm }
109*c7ef0cfcSnicm }
110*c7ef0cfcSnicm }
111*c7ef0cfcSnicm }
112*c7ef0cfcSnicm returnCode(code);
113*c7ef0cfcSnicm }
114*c7ef0cfcSnicm #endif /* _NC_CHECK_MINTTY */
115*c7ef0cfcSnicm
116*c7ef0cfcSnicm #if HAVE_GETTIMEOFDAY == 2
117*c7ef0cfcSnicm #define JAN1970 116444736000000000LL /* the value for 01/01/1970 00:00 */
118*c7ef0cfcSnicm
119*c7ef0cfcSnicm NCURSES_EXPORT(int)
_nc_gettimeofday(struct timeval * tv,void * tz GCC_UNUSED)120*c7ef0cfcSnicm _nc_gettimeofday(struct timeval *tv, void *tz GCC_UNUSED)
121*c7ef0cfcSnicm {
122*c7ef0cfcSnicm union {
123*c7ef0cfcSnicm FILETIME ft;
124*c7ef0cfcSnicm long long since1601; /* time since 1 Jan 1601 in 100ns units */
125*c7ef0cfcSnicm } data;
126*c7ef0cfcSnicm
127*c7ef0cfcSnicm GetSystemTimeAsFileTime(&data.ft);
128*c7ef0cfcSnicm tv->tv_usec = (long) ((data.since1601 / 10LL) % 1000000LL);
129*c7ef0cfcSnicm tv->tv_sec = (long) ((data.since1601 - JAN1970) / 10000000LL);
130*c7ef0cfcSnicm return (0);
131*c7ef0cfcSnicm }
132*c7ef0cfcSnicm #endif // HAVE_GETTIMEOFDAY == 2
133*c7ef0cfcSnicm
134*c7ef0cfcSnicm #endif // _NC_WINDOWS
135