2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-2002
4 * hWaitForInput Runtime Support
7 /* select and supporting types is not Posix */
8 /* #include "PosixSource.h" */
12 * inputReady(fd) checks to see whether input is available on the file
13 * descriptor 'fd'. Input meaning 'can I safely read at least a
14 * *character* from this file object without blocking?'
17 fdReady(int fd, int write, int msecs, int isSock)
20 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
37 /* select() will consider the descriptor set in the range of 0 to
41 tv.tv_sec = msecs / 1000;
42 tv.tv_usec = (msecs % 1000) * 1000;
44 while ((ready = select(maxfd, &rfd, &wfd, NULL, &tv)) < 0 ) {
45 if (errno != EINTR ) {
50 /* 1 => Input ready, 0 => not ready, -1 => error */
53 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
56 HANDLE hFile = (HANDLE)_get_osfhandle(fd);
59 // WaitForMultipleObjects() works for Console input, but it
60 // doesn't work for pipes (it always returns WAIT_OBJECT_0
61 // even when no data is available). There doesn't seem to be
62 // an easy way to distinguish the two kinds of HANDLE, so we
63 // try to detect pipe input first, and if that fails we try
64 // WaitForMultipleObjects().
66 rc = PeekNamedPipe( hFile, NULL, 0, NULL, &avail, NULL );
75 if (rc == ERROR_BROKEN_PIPE) {
76 return 1; // this is probably what we want
78 if (rc != ERROR_INVALID_HANDLE) {
83 rc = WaitForMultipleObjects( 1,
88 /* 1 => Input ready, 0 => not ready, -1 => error */
90 case WAIT_TIMEOUT: return 0;
91 case WAIT_OBJECT_0: return 1;