[project @ 2001-06-28 14:15:04 by simonmar]
[ghc-base.git] / cbits / inputReady.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: inputReady.c,v 1.1 2001/06/28 14:15:04 simonmar Exp $
5  *
6  * hReady Runtime Support
7  */
8
9 /* select and supporting types is not */
10 #ifndef _AIX
11 #define NON_POSIX_SOURCE  
12 #endif
13
14 #include "HsCore.h"
15
16 /*
17  * inputReady(fd) checks to see whether input is available on the file
18  * descriptor 'fd'.  Input meaning 'can I safely read at least a
19  * *character* from this file object without blocking?'
20  */
21 int
22 inputReady(int fd, int msecs)
23 {
24     int maxfd, ready;
25 #ifndef mingw32_TARGET_OS
26     fd_set rfd;
27     struct timeval tv;
28 #endif
29
30 #ifdef mingw32_TARGET_OS
31     return 1;
32 #else
33     FD_ZERO(&rfd);
34     FD_SET(fd, &rfd);
35
36     /* select() will consider the descriptor set in the range of 0 to
37      * (maxfd-1) 
38      */
39     maxfd = fd + 1;
40     tv.tv_sec  = msecs / 1000;
41     tv.tv_usec = msecs % 1000;
42
43     while ((ready = select(maxfd, &rfd, NULL, NULL, &tv)) < 0 ) {
44       if (errno != EINTR ) {
45           return -1;
46       }
47    }
48
49     /* 1 => Input ready, 0 => not ready, -1 => error */
50     return (ready);
51
52 #endif
53 }