----- Forwarded message from Mihail Konev <[email protected]> -----

From: Mihail Konev <[email protected]>
To: David Wohlferd <[email protected]>
Date: Thu, 10 Nov 2016 23:22:48 +0500
Subject: Re: Fwd: [Mingw-w64-public] [PATCH mingw-w64] Add include/iscygtty.c

On Wed, Nov 09, 2016 at 08:21:51PM -0800, David Wohlferd wrote:
> Shouldn't these (and the rest of the chars in this file) be L'\\'?  While it
> seems to work either way for gcc, I'm not sure all compilers are as
> forgiving.
> 
> dw
> 
> -------- Forwarded Message --------
> + if (s0[0] == '\\' && s0[1] == '\\' && s0[2] == '?' && s0[3] == '\\') {
> 
- Then '(int)1 == (long)1' would not be considered correct either.
- Gcc does not complain even with -pedantic -ansi
  (attached are test program source, Makefile, and 'make' output)

It does not harm to check, through, but adding L-s decreases readability.
Attached is v2 patch.
- add L-s
- make "-pty%d-" check stricter; do not check for "-tty%d-"


#include <stdio.h>
#include <wchar.h>

#define PRINT_LONG_EXPR(x) printf(""#x" => %ld\n", (long)(x))

int main(int argc, char **argv) {
        (void) argc; (void) argv;
        PRINT_LONG_EXPR((int)1 == (long)1);
        PRINT_LONG_EXPR((char)1 == (wchar_t)1);
        PRINT_LONG_EXPR('c' == L'c');
        {
                char s[] = "c";
                wchar_t ws[] = L"c";
                PRINT_LONG_EXPR(s[0] == ws[0]);
        }
        return 0;
}

CFLAGS += -Wall -Wextra -O3

all:
        @echo
        $(MAKE) normal
        @echo
        $(MAKE) pedantic
        @echo
        $(MAKE) ansi
        @echo

PHONY: normal
normal:
        $(LINK.c) -o a.exe main.c

PHONY: pedantic
pedantic: CFLAGS += -pedantic
pedantic: CFLAGS += -pedantic-errors
pedantic: normal

PHONY: ansi
ansi: CFLAGS += -ansi
ansi: pedantic



make normal
make[1]: Entering directory '/mnt/j/msys/home/u/int_cast_test'
cc -Wall -Wextra -O3    -o a.exe main.c
make[1]: Leaving directory '/mnt/j/msys/home/u/int_cast_test'

make pedantic
make[1]: Entering directory '/mnt/j/msys/home/u/int_cast_test'
cc -Wall -Wextra -O3 -pedantic -pedantic-errors    -o a.exe main.c
make[1]: Leaving directory '/mnt/j/msys/home/u/int_cast_test'

make ansi
make[1]: Entering directory '/mnt/j/msys/home/u/int_cast_test'
cc -Wall -Wextra -O3 -ansi -pedantic -pedantic-errors    -o a.exe main.c
make[1]: Leaving directory '/mnt/j/msys/home/u/int_cast_test'


>From ed18de062e6dd90d5656f8e74533672958d89a8b Mon Sep 17 00:00:00 2001
From: Mihail Konev <[email protected]>
Date: Tue, 8 Nov 2016 14:16:31 +0500
Subject: [PATCH] Add include/iscygtty.c

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 <[email protected]>
Moved-from: https://github.com/Alexpux/mingw-w64/pull/3
---
 mingw-w64-headers/include/iscygtty.c | 68 ++++++++++++++++++++++++++++++++++++
 1 file changed, 68 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..3eb344bbb142
--- /dev/null
+++ b/mingw-w64-headers/include/iscygtty.c
@@ -0,0 +1,68 @@
+#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'-' || c == 0)
+                    return 1;
+            }
+        }
+    }
+
+    return 0;
+}
+
+#endif /* __ISCYGTTY_C__ */
-- 
2.9.2



----- End forwarded message -----

------------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to