C prgram needed - read with timeout

Gary S. Moss SLCBR-VLD-V moss at BRL.ARPA
Tue Aug 19 00:54:54 AEST 1986


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



More information about the Comp.lang.c mailing list