OpenVMS Source Code Demos

SELECT_DEMO.C

//================================================================================
// title    : select_demo.c
// author 1 : Compaq Computer Corporation
// source   : http://h41379.www4.hpe.com/portability/portingguidelines.html
// notes    : on OpenVMS and VMS, you may not use select() to monitor i/o channels
//            to non-network destinations like stdin, stdout, and stderr.
// author 2 : Neil Rieck
// when     : 2012-03-28
//          : renamed some variables; added extra comments; inserted a few lines
//          : Note: this code demos "bad select 38" as seen in "OpenSSL s_client"
//================================================================================
//
//	import the usual stuff
//
#include <socket.h>						//
#include <stdio.h>						//
#include <stdlib.h>						//
#include <time.h>						//
#include <types.h>						//
#include <unistd.h>						// unix std i/o
#include <errno.h>						// need this for errno (see below)

int  main(void)							//
{
	fd_set		readfds;				// "read" - file descriptor strings
	struct timeval	tv;					//
	int		width;					//
	int		retval;					//
	int		i;					//

	printf("program: select_demo.c\n");			//
	//
	//	"only" watch stdin (fd 0) to see when input is available
	//
	FD_ZERO(&readfds);					// zap all bits in this variable
	FD_SET(0, &readfds);					// set bit associated with stdin
//	FD_SET(1, &readfds);					// set bit associated with stdout
//	FD_SET(2, &readfds);					// set bit associated with stderr
//	FD_SET(3, &readfds);					// set bit associated with network channel x
//	FD_SET(4, &readfds);					// set bit associated with network channel y
	FD_SET(5, &readfds);					// set bit associated with network channel z
	FD_CLR(5, &readfds);					// clear bit associated with network channel z
	//
	//	let's see what is set
	//
	for (i=0;i<31;i++) {
	    if (FD_ISSET(i, &readfds)) {			//
		printf("-i-bit: %d is set\n",i);		//
		width = i + 1;					// width = highest bit plus one
	    }							//
	}							//
	//
	//	We will wait for a maximum of five seconds.
	//
	tv.tv_sec = 5;						// 5 second
	tv.tv_usec = 0;						// 0 microseconds
	//
	width = 1;						// highest bit value plus "one"
	retval = select(width, &readfds, NULL, NULL, &tv);	// the tv parameter is optional
	//
	//	Don't rely on the value of tv now!
	//
	if (retval == -1) {
		printf("-e-just detected error %d\n",errno);	// will throw error 38
	        perror("select()");
		return(2);					// VMS-e
	}
	else if (retval) {
		printf("Data is available now.\n");
		/* FD_ISSET(0, &readfds) will be true. */
	} else {
	        printf("No data within five seconds.\n");
	}
	return(1);						// VMS-s
}