home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume32
/
pol
/
part01
/
pol.lib.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-10-18
|
3KB
|
134 lines
/*
pol.c - poll/select() C interface
Bruce Momjian (root%candle.uucp@bts.com
*/
/* tabs = 4 */
/* Include files */
#include <fcntl.h>
#include <sys/pol.h>
#include <sys/selec.h>
#include <poll.h>
static int pol_fd = -1;
/*---------------------------------------------------------------------------
**
** pol()
**
**--------------------------------------------------------------------------*/
pol(fds, nfds, timeout)
struct pollfd *fds;
unsigned long nfds;
int timeout;
{
struct polfd pl;
int i, ret;
if ( pol_fd == -1 && (pol_fd=open("/dev/pol",O_RDONLY)) == -1)
return -1;
pl.rfds = pl.wfds = 0;
pl.timeout = timeout;
for (i = 0; i < nfds; i++)
{
if ((fds[i].events & POLLIN) != 0)
pl.rfds |= (1 << fds[i].fd);
if ((fds[i].events & POLLOUT) != 0)
pl.wfds |= (1 << fds[i].fd);
}
if ((ret=ioctl(pol_fd, POL_FDS, &pl)) == -1)
return -1;
if (ret == 0 || ret == -1)
pl.rfds = pl.wfds = 0;
for (i=0; i < nfds; i++)
{
fds[i].revents = 0;
if ( (pl.rfds & (1 << fds[i].fd)) != 0)
fds[i].revents |= POLLIN;
if ( (pl.wfds & (1 << fds[i].fd)) != 0)
fds[i].revents |= POLLOUT;
}
return ret;
}
/*---------------------------------------------------------------------------
**
** selec()
**
**--------------------------------------------------------------------------*/
selec(nfds, rfds, wfds, expfds, timeout)
unsigned long nfds, *rfds, *wfds, *expfds; /* exceptions not implemented */
struct timeval *timeout;
{
struct polfd pl;
int i, ored, ret;
if ( pol_fd == -1 && (pol_fd=open("/dev/pol",O_RDONLY)) == -1)
return -1;
pl.rfds = pl.wfds = 0;
if (timeout == 0L)
pl.timeout = -1;
else
pl.timeout = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
for (i = 0; i < nfds; i++)
{
if ( (*rfds & (1 << i)) != 0)
pl.rfds |= 1 << i;
if ( (*wfds & (1 << i)) != 0)
pl.wfds |= 1 << i;
}
if ((ret=ioctl(pol_fd, POL_FDS, &pl)) == -1)
return -1;
*rfds = *wfds = *expfds = 0;
if (ret != 0 && ret != -1)
{
ret = 0;
ored = pl.rfds | pl.wfds;
for (i=0; i < nfds; i++)
{
if ( (pl.rfds & (1 << i)) != 0)
(*rfds) |= (1 << i);
if ( (pl.wfds & (1 << i)) != 0)
(*wfds) |= (1 << i);
if ( (ored & (1 << i)) != 0)
ret++;
}
}
return ret;
}
/*---------------------------------------------------------------------------
**
** polclose()
**
**--------------------------------------------------------------------------*/
polclose()
{
if (pol_fd != -1)
return close(pol_fd);
return 0;
}
/*---------------------------------------------------------------------------
**
** selecclose()
**
**--------------------------------------------------------------------------*/
selecclose()
{
return polclose();
}