Skip to content

Commit 23e71d0

Browse files
committed
Fix test to identify pipes on cygwin/msys, fixes #179
1 parent 0926754 commit 23e71d0

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/main/native/jansi_isatty.c

+20-13
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020

2121
#if defined(_WIN32) || defined(_WIN64)
2222

23-
typedef struct _FILE_NAME_INFORMATION {
24-
uint16_t FileNameLength;
25-
WCHAR FileName[1];
26-
} FILE_NAME_INFORMATION, *PFILE_NAME_INFORMATION;
23+
typedef struct _UNICODE_STRING {
24+
USHORT Length;
25+
USHORT MaximumLength;
26+
PWSTR Buffer;
27+
} UNICODE_STRING, *PUNICODE_STRING;
2728

29+
typedef struct _OBJECT_NAME_INFORMATION {
30+
UNICODE_STRING Name;
31+
WCHAR NameBuffer[0];
32+
} OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION;
2833

2934
typedef enum {
3035
ObjectBasicInformation,
@@ -46,17 +51,17 @@ JNIEXPORT jint JNICALL CLibrary_NATIVE(isatty)
4651

4752
ULONG result;
4853
BYTE buffer[1024];
49-
PFILE_NAME_INFORMATION nameinfo = (PFILE_NAME_INFORMATION) buffer;
54+
POBJECT_NAME_INFORMATION nameinfo = (POBJECT_NAME_INFORMATION) buffer;
5055
PWSTR name;
56+
DWORD mode;
5157

5258
/* check if fd is a pipe */
5359
HANDLE h = (HANDLE) _get_osfhandle(arg0);
5460
DWORD t = GetFileType(h);
5561
if (t == FILE_TYPE_CHAR) {
56-
rc = 1;
57-
}
58-
else if (t != FILE_TYPE_PIPE) {
59-
rc = 0;
62+
// check that this is a real tty because the /dev/null
63+
// and /dev/zero streams are also of type FILE_TYPE_CHAR
64+
rc = GetConsoleMode(h, &mode) != 0;
6065
}
6166
else {
6267
if (hModuleNtDll == 0) {
@@ -78,17 +83,19 @@ JNIEXPORT jint JNICALL CLibrary_NATIVE(isatty)
7883
}
7984
else {
8085

81-
name = nameinfo->FileName;
82-
name[nameinfo->FileNameLength / sizeof(*name)] = 0;
86+
name = nameinfo->Name.Buffer;
87+
name[nameinfo->Name.Length / 2] = 0;
88+
89+
//fprintf( stderr, "Standard stream %d: pipe name: %S\n", arg0, name);
8390

8491
/*
8592
* Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX')
8693
* or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX')
8794
*/
88-
if ((!wcsstr(name, L"msys-") && !wcsstr(name, L"cygwin-"))
89-
|| !wcsstr(name, L"-pty")) {
95+
if ((wcsstr(name, L"msys-") || wcsstr(name, L"cygwin-")) && wcsstr(name, L"-pty")) {
9096
rc = 1;
9197
} else {
98+
// This is definitely not a tty
9299
rc = 0;
93100
}
94101
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)