Timed-out Input

Jan Carlsson janne at enea.se
Sun Mar 4 03:30:14 AEST 1990


In article <25DCE6F4.2232 at paris.ics.uci.edu> bvickers at paris.ics.uci.edu (Brett Joseph Vickers) writes:
>As you can see, I know of no way to time out raw or cbreaked input.
>Is there a way to do this with BSD?

You can use select(2):

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>

main()
{
	struct sgttyb temp;
	fd_set rset;
	struct timeval timeout;
	char c;

	ioctl(0,TIOCGETP,&temp);
	temp.sg_flags |= CBREAK;
	ioctl(0,TIOCSETP,&temp);

	FD_ZERO(&rset);
	FD_SET(0,&rset);
	timeout.tv_sec = 3L;
	timeout.tv_usec = 0L;

	if (select(1,&rset,NULL,NULL,&timeout) == 1 &&
	    FD_ISSET(0,&rset))
		read(0,&c,1);
	else
		printf("Timed out\n");
}
-- 
Jan Carlsson, Enea Data AB, Box 232, Nytorpsvaegen 5, S-183 23 Taeby, Sweden
Phone: +46 8 792 25 00  !  e-mail: janne at enea.se
Fax:   +46 8 768 43 88  !



More information about the Comp.unix.wizards mailing list