Non-block read

Joseph Yip ycy at walt.cc.utexas.edu
Fri Feb 23 05:17:19 AEST 1990


I was trying to do some non-blocking read. I want to program read() so that
what I asked for y/n answer, hitting y or n (without pressing return key) is
enough.

However, the program will wait for a return key to be read although
I have made the read non-blocking.

Any suggestions? How can I solve the problem?

Thanks

Joseph Yip
-------------------------
#include <stdio.h>
#include <fcntl.h>
#include <sys/file.h>

main()
{
	char lbuf[100];
	int n;

	/* make non-blocking */
	if ( (fcntl(0,F_SETFL,fcntl(0,F_GETFL,0) | FNDELAY) == -1) )
		exit(-1);

	/* wait for a key keystroke */
	while ( read(0,lbuf,80) == -1) ;

	/* set blocking read before exit */
	if ( (fcntl(0,F_SETFL,fcntl(0,F_GETFL,0) & ~FNDELAY) == -1) )
		exit(-1);

}



More information about the Comp.lang.c mailing list