X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=cbits%2FinputReady.c;h=f827fe5def79ec1f5deb0a0521f8847281d4bdd4;hb=7c0b04fd273621130062418bb764809c79488dd2;hp=30e140b89c4452cf1fa8a9b7a26ebbcd7494271d;hpb=d13151cc971aaa7400de7c400ad050e8f8fd7a8d;p=haskell-directory.git diff --git a/cbits/inputReady.c b/cbits/inputReady.c index 30e140b..f827fe5 100644 --- a/cbits/inputReady.c +++ b/cbits/inputReady.c @@ -17,10 +17,10 @@ int inputReady(int fd, int msecs, int isSock) { if -#ifndef mingw32_TARGET_OS - ( 1 ) { -#else +#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32) ( isSock ) { +#else + ( 1 ) { #endif int maxfd, ready; fd_set rfd; @@ -45,11 +45,36 @@ inputReady(int fd, int msecs, int isSock) /* 1 => Input ready, 0 => not ready, -1 => error */ return (ready); } -#ifdef mingw32_TARGET_OS +#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32) else { DWORD rc; HANDLE hFile = (HANDLE)_get_osfhandle(fd); - + DWORD avail; + + // WaitForMultipleObjects() works for Console input, but it + // doesn't work for pipes (it always returns WAIT_OBJECT_0 + // even when no data is available). There doesn't seem to be + // an easy way to distinguish the two kinds of HANDLE, so we + // try to detect pipe input first, and if that fails we try + // WaitForMultipleObjects(). + // + rc = PeekNamedPipe( hFile, NULL, 0, NULL, &avail, NULL ); + if (rc != 0) { + if (avail != 0) { + return 1; + } else { + return 0; + } + } else { + rc = GetLastError(); + if (rc == ERROR_BROKEN_PIPE) { + return 1; // this is probably what we want + } + if (rc != ERROR_INVALID_HANDLE) { + return -1; + } + } + rc = WaitForMultipleObjects( 1, &hFile, TRUE, /* wait all */