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