C Input Question?

John Lupien jlup at cci-bdc.UUCP
Thu Feb 28 05:35:36 AEST 1985


> I am working an a very simple game using graphics text with a
> GiGi terminal.  I am having a big problem with the input.  When
> I get to a read, or any input statment, in C, everything stops
> and waits for the input.
> 
[text cut for brevity. Scott wants non-blocking I/O, that is "read if
data, but don't wait for data.]
>
> Is there an easy way to do this in C.  
> I am doing this on a VAX running 4.2 if that makes a difference.  
>
> scott hossler
> rochester!ritcv!sah9577

Scott, your big problem lies in the use of a V7 manual.
Read the 4.2 manual on tty(4) and fcntl(2) for the right way read
without blocking. In brief;

	stty cbreak		/* use ioctl, stty(2), whatever */
	
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>

	res = fcntl (stdin, F_SETFL, FNDELAY);
	
	if (res == -1)
	{
		printf("Can't set up terminal. Goodbye\n");
		exit(1);
	}
	
	if (read (stdin, buf, bytes) == -1)
	{
		if (errno != EWOULDBLOCK)
		{
		printf ("Screwy I/O error: errno = %d. Bombing...\n", 
						errno);
			exit(1);
		}
		noread = TRUE;
	} else
	{
		noread = FALSE;
	}
	
	if (noread)
	{
		/* do processing for *no input* condition */
	} else
	{
		/* process input in buf. */
	}
	
  Although the above is not in any sense a legal program, this is the 
  kind of thing you need to do on 4.2 to get the functionality you 
  describe. I hope I understood your needs, but this should be of
  general interest to BSD users anyway.

		-John Lupien
		CCI Boston Development Center
		222 Third St.
		Cambridge,MA 02142

<imaginary data>



More information about the Comp.lang.c mailing list