printf, data presentation

Piet van Oostrum piet at ruuinf
Tue Jan 10 20:09:28 AEST 1989


In article <8800006 at gistdev>, flint at gistdev writes:

          to request a _portable_ way to do efficient timed reads. 

Here is a routine I have used for this: It is robust in that it allows
other sleep/alarms in the same program. There's also a routine to do a
timed open (e.g. for terminal lines):


int oldalarm ;
int newalarm ;
int (*oldsig)();

timed_read_alarm()
{
	signal (SIGALRM, oldsig);
	if ( oldalarm != 0 ) {
	  if (oldalarm <= newalarm) kill (getpid(), SIGALRM);
	  else alarm (oldalarm-newalarm);
	} else alarm(0);
	oldalarm = newalarm = 0;
}

timed_read (fd, buf, size, time)
int fd, size, time; char *buf;
{
	long i, rl;
#ifdef BSD
	sleep (time);
	ioctl (fd, FIONREAD, &rl);
	if (rl >= size) rl = size-1;
	if (rl > 0) rl = read (fd, buf, rl);
#else	
	newalarm = time;
	oldalarm = alarm (10000);
	oldsig = signal (SIGALRM, timed_read_alarm);
	sleep (time-1);
	alarm (1);
	rl = (read (fd, buf, size-1));
	if (newalarm != 0) timed_read_alarm();
#endif	
	return (rl);
}

FILE *timed_open (dev, time)
char* dev; int time;
{
        FILE *fw = NULL;
  	newalarm = time;
	oldalarm = alarm (10000);
	oldsig = signal (SIGALRM, timed_read_alarm);
	alarm (time);
	fw = fopen (dev, "w+");
	if (newalarm != 0) timed_read_alarm();
	return (fw);
}
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806        UUCP: ...!mcvax!hp4nl!ruuinf!piet



More information about the Comp.unix.wizards mailing list