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