/*
 * selectExample.c
 *
 * select example ...
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>

#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>

/*
 * bit masks for manipulating select masks.
 * (may be available somewhere in include files; look around)
 *
 * On sun: read sys/types.h...
 *
 *	set - set one bit
 *	clr - clear one bit
 *	isset - test one bit
 *	zero - zero the mask
 */
#define	FD_SET(n, p)	((p)->fds_bits[0] |= (1<<(n)))
#define	FD_CLR(n, p)	((p)->fds_bits[0] &= ~(1<<(n)))
#define	FD_ISSET(n, p)	((p)->fds_bits[0] & (1<<(n)))
#define FD_ZERO(p)	((p)->fds_bits[0] = 0)


doSelect (sock, tty)
int sock;
int tty;
{
	int noquit = 0;		/* number of connections terminated */
	static struct timeval timeout = { 0 };
	extern int errno;
	int newsock;
	fd_set	readbits;	/* read mask for select input */
	fd_set	readers;	/* mask for current connected sockets */
	int i;
	struct sockaddr_in peer;
	int size;
	int rc;

	while(1) {

		FD_ZERO(&readbits);
		FD_SET(sock, &readbits);
		FD_SET(tty, &readbits);

		/******************************************* 
		 * make select call
		 */
		rc = select(getdtablesize(), (fd_set *)&readbits, 
				(fd_set *)0, 
				(fd_set *)0, 
				0);
				/*
				&timeout);
				*/

		if ( rc == 0 ) {
			timeout...
		}
		else if (rc < 0) {
			if (errno == EINTR) {
					continue;
			}
			fprintf(stderr,"select error %s %d\n", __FILE__, __LINE__);
			perror("select");
			exit(1);
		}

		/*
		 * check listenSocket first for accepted connections
	         */ 
		if (FD_ISSET(tty, &readbits)) {
			read on tty...
			break
		}
		if (FD_ISSET(sock, &readbits)) {
			read on tty...
			break
		}
	}
}


