X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=cbits%2FinputReady.c;h=2949e9494bbef90696f4c1d613018dc5b9f3cf30;hb=92fd355ddd1360f3c823457b8f112b673d64e72b;hp=5b1e005ed4a1d4c1c8ac056a16683112b2def8cf;hpb=b2e1bc7e50a23d746911ce8eaf75debcfb366f17;p=haskell-directory.git diff --git a/cbits/inputReady.c b/cbits/inputReady.c index 5b1e005..2949e94 100644 --- a/cbits/inputReady.c +++ b/cbits/inputReady.c @@ -17,7 +17,7 @@ int inputReady(int fd, int msecs, int isSock) { if -#ifndef mingw32_TARGET_OS +#ifndef mingw32_HOST_OS ( 1 ) { #else ( isSock ) { @@ -34,7 +34,7 @@ inputReady(int fd, int msecs, int isSock) */ maxfd = fd + 1; tv.tv_sec = msecs / 1000; - tv.tv_usec = msecs % 1000; + tv.tv_usec = (msecs % 1000) * 1000; while ((ready = select(maxfd, &rfd, NULL, NULL, &tv)) < 0 ) { if (errno != EINTR ) { @@ -45,16 +45,40 @@ inputReady(int fd, int msecs, int isSock) /* 1 => Input ready, 0 => not ready, -1 => error */ return (ready); } -#ifdef mingw32_TARGET_OS +#ifdef mingw32_HOST_OS else { DWORD rc; HANDLE hFile = (HANDLE)_get_osfhandle(fd); - - rc = MsgWaitForMultipleObjects( 1, - &hFile, - FALSE, /* wait all */ - msecs, /*millisecs*/ - QS_ALLEVENTS); + 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 */ + msecs); /*millisecs*/ /* 1 => Input ready, 0 => not ready, -1 => error */ switch (rc) {