Date: Mon, 3 Jun 1996 16:33:01 +0400 (MSD) From: "Alexander V. Lukyanov" Message-Id: <199606031233.QAA12680@video.yars.free.net> To: djgpp-workers AT delorie DOT com Subject: poll() Here is a implementation of poll() via select(). It slightly incompatible with SysV one, but works in most cases. It never returns POLLNVAL, POLLERR and POLLHUP because of select limitations. Instead of marking a file descriptor with POLLNVAL it returns -1 and sets errno to EBADF, just as select does. #!/bin/sh # This is a shell archive (produced by GNU shar 4.0). # To extract the files from this archive, save it to some FILE, remove # everything before the `!/bin/sh' line above, then type `sh FILE'. # # Made on 1996-06-03 13:54 MSK by . # Source directory was `/home/lav/le/poll'. # # Existing files will *not* be overwritten unless `-c' is specified. # # This shar contains: # length mode name # ------ ---------- ------------------------------------------ # 848 -rw-r--r-- poll.c # 1502 -rw-r--r-- poll.h # touch -am 1231235999 $$.touch >/dev/null 2>&1 if test ! -f 1231235999 && test -f $$.touch; then shar_touch=touch else shar_touch=: echo 'WARNING: not restoring timestamps' fi rm -f 1231235999 $$.touch # # ============= poll.c ============== if test -f 'poll.c' && test X"$1" != X"-c"; then echo 'x - skipping poll.c (File already exists)' else echo 'x - extracting poll.c (text)' sed 's/^X//' << 'SHAR_EOF' > 'poll.c' && #include "poll.h" #include #include #include X int poll(struct pollfd *pfd,unsigned long nfd,int timeout) { X fd_set in,out,pri; X struct timeval tv; X int n=0; X int i; X X FD_ZERO(&in); X FD_ZERO(&out); X FD_ZERO(&pri); X X for(i=0; i=n) X n++; X } X tv.tv_sec=timeout/1000; X tv.tv_usec=(timeout%1000)*1000; X X n=select(n,&in,&out,&pri,timeout==-1?NULL:&tv); X X if(n==-1) X return(-1); X X for(i=0; i 'poll.h' && /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ /* All Rights Reserved */ X /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */ /* The copyright notice above does not evidence any */ /* actual or intended publication of such source code. */ X #ifndef _SYS_POLL_H #define _SYS_POLL_H X #pragma ident "@(#)poll.h 1.18 94/06/07 SMI" /* SVr4.0 11.9 */ X #ifdef __cplusplus extern "C" { #endif X /* X * Structure of file descriptor/event pairs supplied in X * the poll arrays. X */ typedef struct pollfd { X int fd; /* file desc to poll */ X short events; /* events of interest on fd */ X short revents; /* events that occurred on fd */ } pollfd_t; X /* X * Testable select events X */ #define POLLIN 0x0001 /* fd is readable */ #define POLLPRI 0x0002 /* high priority info at fd */ #define POLLOUT 0x0004 /* fd is writeable (won't block) */ #define POLLRDNORM 0x0040 /* normal data is readable */ #define POLLWRNORM POLLOUT #define POLLRDBAND 0x0080 /* out-of-band data is readable */ #define POLLWRBAND 0x0100 /* out-of-band data is writeable */ X #define POLLNORM POLLRDNORM X /* X * Non-testable poll events (may not be specified in events field, X * but may be returned in revents field). X */ #define POLLERR 0x0008 /* fd has error condition */ #define POLLHUP 0x0010 /* fd has been hung up on */ #define POLLNVAL 0x0020 /* invalid pollfd entry */ X #if defined(__STDC__) int poll(struct pollfd *, unsigned long, int); #endif X #ifdef __cplusplus } #endif X #endif /* _SYS_POLL_H */ SHAR_EOF $shar_touch -am 1108140795 'poll.h' && chmod 0644 'poll.h' || echo 'restore of poll.h failed' shar_count="`wc -c < 'poll.h'`" test 1502 -eq "$shar_count" || echo "poll.h: original size 1502, current size $shar_count" fi exit 0