www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1996/06/03/08:36:28

Date: Mon, 3 Jun 1996 16:33:01 +0400 (MSD)
From: "Alexander V. Lukyanov" <lav AT video DOT yars DOT free DOT net>
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 <lav AT video>.
# 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 <sys/types.h>
#include <time.h>
#include <sys/time.h>
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<nfd; i++)
X	{
X		if(pfd[i].events&POLLIN)
X			FD_SET(pfd[i].fd,&in);
X		if(pfd[i].events&POLLOUT)
X			FD_SET(pfd[i].fd,&out);
X		if(pfd[i].events&POLLPRI)
X			FD_SET(pfd[i].fd,&pri);
X		if(pfd[i].fd>=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<nfd; i++)
X	{
X		pfd[i].revents=0;
X		if(FD_ISSET(pfd[i].fd,&in))
X			pfd[i].revents|=POLLIN;
X		if(FD_ISSET(pfd[i].fd,&out))
X			pfd[i].revents|=POLLOUT;
X		if(FD_ISSET(pfd[i].fd,&pri))
X			pfd[i].revents|=POLLPRI;
X	}
X	
X	return(n);
}
SHAR_EOF
  $shar_touch -am 1108193295 'poll.c' &&
  chmod 0644 'poll.c' ||
  echo 'restore of poll.c failed'
  shar_count="`wc -c < 'poll.c'`"
  test 848 -eq "$shar_count" ||
    echo "poll.c: original size 848, current size $shar_count"
fi
# ============= poll.h ==============
if test -f 'poll.h' && test X"$1" != X"-c"; then
  echo 'x - skipping poll.h (File already exists)'
else
  echo 'x - extracting poll.h (text)'
  sed 's/^X//' << 'SHAR_EOF' > '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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019