Path: utzoo!attcan!uunet!mcvax!hp4nl!ruuinf!piet From: piet@ruuinf (Piet van Oostrum) Newsgroups: comp.unix.wizards Subject: Re: printf, data presentation Message-ID: <961@ruuinf.UUCP> Date: 10 Jan 89 10:09:28 GMT References: <19@xenlink.UUCP> <8800006@gistdev> Sender: piet@ruuinf.UUCP Reply-To: piet@ruuinf (Piet van Oostrum) Organization: Dept of Computer Science, University of Utrecht, Holland Lines: 60 In-reply-to: flint@gistdev.UUCP In article <8800006@gistdev>, flint@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