[comp.lang.c] Non-block read

ycy@walt.cc.utexas.edu (Joseph Yip) (02/23/90)

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);

}

CMH117@psuvm.psu.edu (Charles Hannum) (02/23/90)

The "cbreak()" function sets the terminal to raw i/o mode, which allows
getchar() to get one character at a time.

Virtually,
- Charles Martin Hannum II       "Klein bottle for sale ... inquire within."
    (That's Charles to you!)     "To life immortal!"
  cmh117@psuvm.{bitnet,psu.edu}  "No noozzzz izzz netzzzsnoozzzzz..."
  c9h@psuecl.{bitnet,psu.edu}    "Mem'ry, all alone in the moonlight ..."