2 * poll2select - convert poll() calls to select() calls
3 * Copyright 2005 Ludovico Gardenghi
4 * Licensed under the GPLv2
8 #include <sys/select.h>
10 #include <sys/types.h>
18 #include "poll2select.h"
21 #define MAX(x,y) ((x)>(y)?(x):(y))
24 static int prepare_select(struct pollfd
*ufds
, nfds_t nfds
, int timeout
,
25 struct timeval
**pstimeout
, int *maxfdp1
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*efds
)
28 struct pollfd
*currfd
;
29 struct timeval
*stimeout
= *pstimeout
;
32 * Conversion of information about file descriptors
37 if ((nfds
> 0) && (ufds
== NULL
))
43 for (i
= 0; i
< nfds
; i
++)
53 if (currfd
->events
& POLLIN
)
54 FD_SET(currfd
->fd
, rfds
);
55 if (currfd
->events
& POLLOUT
)
56 FD_SET(currfd
->fd
, wfds
);
57 if (currfd
->events
& POLLPRI
)
58 FD_SET(currfd
->fd
, efds
);
60 *maxfdp1
= MAX(*maxfdp1
, currfd
->fd
);
66 * Conversion of information about timeout
77 stimeout
->tv_usec
= 0;
86 stimeout
->tv_sec
= timeout
/ 1000;
87 stimeout
->tv_usec
= (timeout
% 1000) * 1000;
89 else // if (timeout < 0)
95 static int convert_results(struct pollfd
*ufds
, int nfds
,
96 fd_set
*rfds
, fd_set
*wfds
, fd_set
*efds
)
99 struct pollfd
*currfd
;
102 for (i
= 0; i
< nfds
; i
++)
108 if (FD_ISSET(currfd
->fd
, rfds
))
109 currfd
->revents
|= POLLIN
;
110 if (FD_ISSET(currfd
->fd
, wfds
))
111 currfd
->revents
|= POLLOUT
;
112 if (FD_ISSET(currfd
->fd
, efds
))
113 currfd
->revents
|= POLLPRI
;
115 if (currfd
->revents
!= 0)
122 int poll2select(struct pollfd
*ufds
, nfds_t nfds
, int timeout
)
124 fd_set rfds
, wfds
, efds
;
125 struct timeval stimeout
;
126 struct timeval
*pstimeout
= &stimeout
;
128 int pretval
, sretval
, tretval
;
134 tretval
= prepare_select(ufds
, nfds
, timeout
, &pstimeout
, &maxfdp1
, &rfds
, &wfds
, &efds
);
138 sretval
= select(maxfdp1
, &rfds
, &wfds
, &efds
, pstimeout
);
142 pretval
= convert_results(ufds
, nfds
, &rfds
, &wfds
, &efds
);