This is the mail archive of the cygwin-developers mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

is-cygwin-tty check


A function for mingw-w64 is being proposed,
that would distinguish between an ordinary named pipe,
and a mintty-like stdin.

(Needed for mingw-w64 packages that rely on isatty()/fstat(stdin)
to work correctly if they are launched not from cmd.exe console).

Currently, it checks the ObjectNameInformation for
- possible "\\?\" in front of string
- "\Device\NamedPipe\"
- any characters afterwards
- "-pty[0-9]+-"
- allowing anything afterwards

Is the check correct?

In particular, should it put any restrictions
on what is before and after the -pty%d- (slashes, non-[-a-z0-9] ?)
--- Begin Message ---
Applications now could call iscygtty(STDIN_FILENO)
in order to detect whether they are running from
Cygwin/MSys terminal.

Without that, they have no choice but to think that
stdin is redirected from a named pipe.

Signed-off-by: Mihail Konev <k.mvc@ya.ru>
Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
---
Sorry, the v3 "\\.\" check is incorrect.

A
  \\.\pipe\*-pty%d-*
should be looked for, not the
  \\?\Device\NamedPipe\*-pty%d-*
.

Reverted the change, as the old way, it still works.

 mingw-w64-headers/include/iscygtty.c | 69 ++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 mingw-w64-headers/include/iscygtty.c

diff --git a/mingw-w64-headers/include/iscygtty.c b/mingw-w64-headers/include/iscygtty.c
new file mode 100644
index 000000000000..51e8b0e7319b
--- /dev/null
+++ b/mingw-w64-headers/include/iscygtty.c
@@ -0,0 +1,69 @@
+#ifndef __ISCYGTTY_C__
+#define __ISCYGTTY_C__
+
+#include <io.h>
+#include <winternl.h>
+
+static int iscygtty(int fd) {
+    intptr_t h_fd = _get_osfhandle(fd);
+
+    char ntfn_bytes[sizeof(OBJECT_NAME_INFORMATION) + 256 * sizeof(WCHAR)];
+    OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION*) ntfn_bytes;
+    NTSTATUS status;
+    ULONG ntfn_size = sizeof(ntfn_bytes);
+
+    USHORT i, l;
+    wchar_t c, *s0;
+
+    memset(ntfn, 0, ntfn_size);
+    status = NtQueryObject((HANDLE)h_fd, ObjectNameInformation,
+            ntfn, ntfn_size, &ntfn_size);
+
+    if (!NT_SUCCESS(status))
+        return 0;
+
+    l = ntfn->Name.Length;
+    s0 = ntfn->Name.Buffer;
+
+    /* Check for "\Device\NamedPipe" */
+    {
+        USHORT l1 = l;
+        wchar_t *s1 = s0;
+        wchar_t expect[] = L"\\Device\\NamedPipe\\";
+
+        if (s0[0] == L'\\' && s0[1] == L'\\' && s0[2] == L'?' && s0[3] == L'\\') {
+            l1 -= 4;
+            s1 += 4;
+        }
+
+        for (i = 0; i < l1; i++) {
+            wchar_t e = expect[i];
+            c = s1[i];
+            if (!e)
+                break;
+            if (c != e)
+                return 0;
+        }
+    }
+
+    /* Look for "-pty%d-" */
+    for (i = 0; i < l; i++) {
+        c = s0[i];
+        if (c == L'-') {
+            wchar_t *s = s0 + i + 1;
+            if (s[0] == L'p' && s[1] == L't' && s[2] == L'y' &&
+                    (c = s[3]) && (c >= L'0') && (c <= L'9'))
+            {
+                s += 4;
+                while ((c = *s) && (c >= L'0') && (c <= L'9'))
+                    s++;
+                if (c == L'-')
+                    return 1;
+            }
+        }
+    }
+
+    return 0;
+}
+
+#endif /* __ISCYGTTY_C__ */
-- 
2.9.2


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

--- End Message ---

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]