/* ** poll()-like function */ #ifdef PSEUDOPOLL #include #include #include #include #include #include #include #ifdef NEED_SELECT_H #include #endif #define BITSPERBYTE 8 static char *readfds_c=NULL; static char *writefds_c=NULL; static int fdssize=0; static int fdsbytes=0; inline int pseudopoll(struct pollfd *fds,unsigned long nfds,int timeout){ int i,j,s,newfdsbytes,omitfd,byteposition,currfd,maxfd1=0; char *newreadfds_c,*newwritefds_c,bit; struct timeval tim; if(!readfds_c){ fdsbytes=256/BITSPERBYTE; fdssize=256; readfds_c=(char*)malloc(fdsbytes); writefds_c=(char*)malloc(fdsbytes); if(!readfds_c||!writefds_c) abort(); } bzero(readfds_c,fdsbytes); bzero(writefds_c,fdsbytes); for(i=0;i=0){ currfd=fds[i].fd; omitfd=0; if(currfd>=maxfd1) maxfd1=currfd+1; if(currfd>=fdssize){ newfdsbytes=currfd/BITSPERBYTE+1; newreadfds_c=(char*)realloc(readfds_c,newfdsbytes); newwritefds_c=(char*)realloc(writefds_c,newfdsbytes); if(newreadfds_c&&newwritefds_c){ readfds_c=newreadfds_c; writefds_c=newwritefds_c; s=newfdsbytes-fdsbytes; bzero(readfds_c+fdsbytes,s); bzero(writefds_c+fdsbytes,s); fdssize=newfdsbytes*BITSPERBYTE; fdsbytes=newfdsbytes; }else{ omitfd=1; } } if(!omitfd){ byteposition=currfd/BITSPERBYTE; bit=1<<(currfd%BITSPERBYTE); if(fds[i].events&POLLIN) readfds_c[byteposition]|=bit; if(fds[i].events&POLLOUT) writefds_c[byteposition]|=bit; } } } tim.tv_sec=timeout/1000; tim.tv_usec=(unsigned long)(timeout%1000)*1000; if((j=select(maxfd1,(fd_set*)readfds_c,(fd_set*)writefds_c,NULL,&tim)>0)){ for(i=0;i=0){ byteposition=fds[i].fd/BITSPERBYTE; bit=1<<(fds[i].fd%BITSPERBYTE); if((readfds_c[byteposition]&bit)&&(fds[i].events&POLLIN)) fds[i].revents|=POLLIN; if((writefds_c[byteposition]&bit)&&(fds[i].events&POLLOUT)) fds[i].revents|=POLLOUT; } } }else{ for(i=0;i