[net.lang.c] C prgram needed - read with timeout

02335%UWAV4.BITNET@wiscvm.ARPA (08/16/86)

Hi,

I have been working on an HP-9000 Unix machine trying to do a read that
does not wait for a RETURN.  One character is all I need, but there doesn;t
seem to be a C function to do this.  I've looked at the IOCTL stuff, but the
timeout doesn't work until after one character has been pressed.  I did
get something to work setting the O_NDELAY bit with FCNTL, but there is no
way to unset that bit so when I exit the program I get logged out since
UNIX timeouts and all other reads timeout as well.

I eventually wrote an interrupt handler using SIGNAL.  But I really
need to trap and return any character being pressed not just the DELETE key.

My main program is really in FORTRAN, but I'm pretty sure that I must
call a C routine to get the job done if it is possible.  I am really a
Vax FORTRAN programmer which has no problem polling the keypboard, so I
am a little amused that C doesn't seem to have this capability.

Any help would be greatly appreciated...  Please reply directly to me since
I do not get INFO-C mail.

Thanks in advance,
Tony Andrea
Sierra Geophysics, Inc.
Kirkland, Washington

BITNET:      02335 at UWAV4
ARPA/CSNET:  02335%uwav4.bitnet@wiscvm.arpa

moss@BRL.ARPA (Gary S. Moss (SLCBR-VLD-V)) (08/18/86)

Tony,
	When I use fcntl() to set O_NDELAY, I save the file status flags on
program startup, and restore them before exiting.  Here are some subroutines
out of my library that should do the job, assuming that HP's UNIX is pretty
standard...
#include <stdio.h>
#include <fcntl.h>
static int		fileStatus[_NFILE];
int			reset_Fil_Stat();

/*	s a v e _ F i l _ S t a t ( )
	Save file status flags for 'fd'.
 */
save_Fil_Stat( fd )
int	fd;
	{
	return	fileStatus[fd] = fcntl( fd, F_GETFL, 0 );
	}

/*	r e s e t _ F i l _ S t a t ( )
	Restore file status flags for file desc. 'fd' to what they were the
	last time save_Fil_Stat(fd) was called.
 */
reset_Fil_Stat( fd )
int	fd;
	{
	return	fcntl( fd, F_SETFL, fileStatus[fd] );
	}

/*	s e t _ O _ N D E L A Y ( )
	Set non-blocking read on 'fd'.
 */
set_O_NDELAY( fd )
int	fd;
	{
	return	fcntl( fd, F_SETFL, O_NDELAY );
	}
-moss