]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_win32util.c
ncurses 6.2 - patch 20200907
[ncurses.git] / ncurses / tinfo / lib_win32util.c
1 /****************************************************************************
2  * Copyright 2020 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
37 MODULE_ID("$Id: lib_win32util.c,v 1.1 2020/08/14 21:57:01 juergen 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 it's 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      Althogh 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  */
61 NCURSES_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(
83                                                  PROCESS_CREATE_THREAD
84                                                  | PROCESS_QUERY_INFORMATION
85                                                  | PROCESS_VM_OPERATION
86                                                  | PROCESS_VM_WRITE
87                                                  | PROCESS_VM_READ,
88                                                  FALSE,
89                                                  pPid);
90                     if (pMinTTY)
91                         *pMinTTY = INVALID_HANDLE_VALUE;
92                     if (pHandle != INVALID_HANDLE_VALUE) {
93                         if ((len = GetProcessImageFileName(
94                                                            pHandle,
95                                                            buf,
96                                                            (DWORD)
97                                                            array_length(buf)))) {
98                             TCHAR *pos = _tcsrchr(buf, _T('\\'));
99                             if (pos) {
100                                 pos++;
101                                 if (_tcsnicmp(pos, _TEXT("mintty.exe"), 10)
102                                     == 0) {
103                                     if (pMinTTY)
104                                         *pMinTTY = pHandle;
105                                     code = 1;
106                                 }
107                             }
108                         }
109                     }
110                 }
111             }
112         }
113     }
114     returnCode(code);
115 }
116 #endif /* _NC_CHECK_MINTTY */
117 \f
118 #define JAN1970 116444736000000000LL    /* the value for 01/01/1970 00:00 */
119
120 NCURSES_EXPORT(int)
121 _nc_gettimeofday(struct timeval *tv, void *tz GCC_UNUSED)
122 {
123     union {
124         FILETIME ft;
125         long long since1601;    /* time since 1 Jan 1601 in 100ns units */
126     } data;
127
128     GetSystemTimeAsFileTime(&data.ft);
129     tv->tv_usec = (long) ((data.since1601 / 10LL) % 1000000LL);
130     tv->tv_sec = (long) ((data.since1601 - JAN1970) / 10000000LL);
131     return (0);
132 }
133
134 #endif // _NC_WINDOWS