1/****************************************************************************
2 * Copyright 2020-2021,2023 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
37MODULE_ID("$Id: lib_win32util.c,v 1.1 2023/10/17 09:52:09 nicm Exp $")
38
39#ifdef _NC_WINDOWS
40#include <io.h>
41
42#ifdef _NC_CHECK_MINTTY
43#define PSAPI_VERSION 2
44#include <psapi.h>
45#include <tchar.h>
46
47#define array_length(a) (sizeof(a)/sizeof(a[0]))
48
49/*   This function tests, whether or not the ncurses application
50     is running as a descendant of MSYS2/cygwin mintty terminal
51     application. mintty doesn't use Windows Console for its screen
52     I/O, so the native Windows _isatty doesn't recognize it as
53     character device. But we can discover we are at the end of an
54     Pipe and can query the server side of the pipe, looking whether
55     or not this is mintty.
56     For now we terminate the program if we discover that situation.
57     Although in theory it would be possible, to remotely manipulate
58     the terminal state of mintty, this is out of scope for now and
59     not worth the significant effort.
60 */
61NCURSES_EXPORT(int)
62_nc_console_checkmintty(int fd, LPHANDLE pMinTTY)
63{
64    HANDLE handle = _nc_console_handle(fd);
65    DWORD dw;
66    int code = 0;
67
68    T((T_CALLED("lib_winhelper::_nc_console_checkmintty(%d, %p)"), fd, pMinTTY));
69
70    if (handle != INVALID_HANDLE_VALUE) {
71	dw = GetFileType(handle);
72	if (dw == FILE_TYPE_PIPE) {
73	    if (GetNamedPipeInfo(handle, 0, 0, 0, 0)) {
74		ULONG pPid;
75		/* Requires NT6 */
76		if (GetNamedPipeServerProcessId(handle, &pPid)) {
77		    TCHAR buf[MAX_PATH];
78		    DWORD len = 0;
79		    /* These security attributes may allow us to
80		       create a remote thread in mintty to manipulate
81		       the terminal state remotely */
82		    HANDLE pHandle = OpenProcess(PROCESS_CREATE_THREAD
83						 | PROCESS_QUERY_INFORMATION
84						 | PROCESS_VM_OPERATION
85						 | PROCESS_VM_WRITE
86						 | PROCESS_VM_READ,
87						 FALSE,
88						 pPid);
89		    if (pMinTTY)
90			*pMinTTY = INVALID_HANDLE_VALUE;
91		    if (pHandle != INVALID_HANDLE_VALUE) {
92			if ((len = GetProcessImageFileName(pHandle,
93							   buf,
94							   (DWORD)
95							   array_length(buf)))) {
96			    TCHAR *pos = _tcsrchr(buf, _T('\\'));
97			    if (pos) {
98				pos++;
99				if (_tcsnicmp(pos, _TEXT("mintty.exe"), 10)
100				    == 0) {
101				    if (pMinTTY)
102					*pMinTTY = pHandle;
103				    code = 1;
104				}
105			    }
106			}
107		    }
108		}
109	    }
110	}
111    }
112    returnCode(code);
113}
114#endif /* _NC_CHECK_MINTTY */
115
116#if HAVE_GETTIMEOFDAY == 2
117#define JAN1970 116444736000000000LL	/* the value for 01/01/1970 00:00 */
118
119NCURSES_EXPORT(int)
120_nc_gettimeofday(struct timeval *tv, void *tz GCC_UNUSED)
121{
122    union {
123	FILETIME ft;
124	long long since1601;	/* time since 1 Jan 1601 in 100ns units */
125    } data;
126
127    GetSystemTimeAsFileTime(&data.ft);
128    tv->tv_usec = (long) ((data.since1601 / 10LL) % 1000000LL);
129    tv->tv_sec = (long) ((data.since1601 - JAN1970) / 10000000LL);
130    return (0);
131}
132#endif // HAVE_GETTIMEOFDAY == 2
133
134#endif // _NC_WINDOWS
135