* include/ruby/io.h (rb_io_t): new fields: writeconv,
[ruby-svn.git] / x68 / select.c
blobb4bf46403243a4b2b36b361260b36ab091b469bd
1 /*
2 * PROJECT C Library, X68000 PROGRAMMING INTERFACE DEFINITION
3 * --------------------------------------------------------------------
4 * This file is written by the Project C Library Group, and completely
5 * in public domain. You can freely use, copy, modify, and redistribute
6 * the whole contents, without this notice.
7 * --------------------------------------------------------------------
8 * $Id$
9 */
11 #ifndef __IOCS_INLINE__
12 #define __IOCS_INLINE__
13 #define __DOS_INLINE__
14 #define __DOS_DOSCALL__
15 #endif
17 /* System headers */
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <sys/dos.h>
22 #include <sys/iocs.h>
23 #include <sys/time.h>
24 #include <sys/types.h>
25 #if 0
26 #include <sys/select.h>
27 #include <sys/xsocket.h>
28 #endif
29 #include <sys/xunistd.h>
31 /* Macros */
32 #define XFD_ISSET(fd,fds) ((fds) && FD_ISSET ((fd), (fds)))
33 #define isreadable(mode) ((mode) == O_RDONLY || (mode) == O_RDWR)
34 #define iswritable(mode) ((mode) == O_WRONLY || (mode) == O_RDWR)
35 #ifndef _POSIX_FD_SETSIZE
36 #define _POSIX_FD_SETSIZE OPEN_MAX
37 #endif
39 /* Functions */
40 int
41 select (int fds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
43 fd_set oread, owrite, oexcept;
44 int ticks, start;
45 int nfds;
47 if (fds > _POSIX_FD_SETSIZE)
49 errno = EINVAL;
50 return -1;
53 FD_ZERO (&oread);
54 FD_ZERO (&owrite);
55 FD_ZERO (&oexcept);
57 nfds = 0;
58 ticks = -1;
60 if (timeout)
62 ticks = timeout->tv_sec * 100 + timeout->tv_usec / 10000;
63 if (ticks < 0)
65 errno = EINVAL;
66 return -1;
70 start = _iocs_ontime ();
71 for (;;)
74 int fd;
76 for (fd = 0; fd < fds; fd++)
78 int accmode;
80 if (_fddb[fd].inuse == _FD_NOTUSED)
81 continue;
83 accmode = _fddb[fd].oflag & O_ACCMODE;
85 if (isatty (fd))
87 if (XFD_ISSET (fd, rfds) && isreadable (accmode) && _dos_k_keysns ())
89 FD_SET (fd, &oread);
90 nfds++;
93 if (XFD_ISSET (fd, wfds) && iswritable (accmode))
95 FD_SET (fd, &owrite);
96 nfds++;
99 #if 0
100 else if (_fddb[fd].sockno >= 0)
102 if (XFD_ISSET (fd, rfds) && _socklen (_fddb[fd].sockno, 0))
104 FD_SET (fd, &oread);
105 nfds++;
108 if (XFD_ISSET (fd, wfds) /* && _socklen (_fddb[fd].sockno, 1) == 0 */)
110 FD_SET (fd, &owrite);
111 nfds++;
114 #endif
115 else
117 if (XFD_ISSET (fd, rfds) && isreadable (accmode) && _dos_ioctrlis (fd))
119 FD_SET (fd, &oread);
120 nfds++;
123 if (XFD_ISSET (fd, wfds) && iswritable (accmode) && _dos_ioctrlos (fd))
125 FD_SET (fd, &owrite);
126 nfds++;
133 int rest;
135 if ((rest = (_iocs_ontime () - start) % 8640000) < 0)
136 rest += 8640000;
138 if (nfds != 0)
140 if (ticks >= 0)
142 int left;
144 if ((left = ticks - rest) < 0)
145 left = 0;
147 timeout->tv_sec = left / 100;
148 timeout->tv_usec = (left % 100) * 10000;
151 if (rfds)
152 *rfds = oread;
153 if (wfds)
154 *wfds = owrite;
155 if (efds)
156 *efds = oexcept;
158 return nfds;
161 if (ticks >= 0 && rest > ticks)
162 return 0;
165 _dos_change_pr ();