Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bbn!apple!oliveb!ames!ucsd!rutgers!att!mtunb!jcm From: jcm@mtunb.ATT.COM (was-John McMillan) Newsgroups: comp.unix.wizards Subject: Re: help needed on a unix system-call question Keywords: signal, system-call, read, unix v Message-ID: <1431@mtunb.ATT.COM> Date: 10 Mar 89 14:49:18 GMT References: <2100@iitmax.IIT.EDU> <1134@auspex.UUCP> Reply-To: jcm@mtunb.UUCP (John McMillan) Organization: AT&T ISL Middletown NJ USA Lines: 60 In article <1134@auspex.UUCP> guy@auspex.UUCP (Guy Harris) writes: >>In my code I use a signal (SIGALRM), system-call, following which is a 'read' >>... >... >for more data, and *then* gets interrupted by a signal, is the data it >transferred more-or-less lost", the answer is "yes, and this happens >under 4.xBSD as well". The problem is that, since you get a -1 back >from the "read", rather than a count of the number of bytes read, you >have no idea how much data was actually transferred. "More or less lost"? "Unnoticed" may be more like it, IF MY RECOLLECTIONS ARE CORRECT. (When did that last occur?) I can't RECALL any place for the characters to BE lost (in traditional tty reads -- as opposed to STREAMS, which I've paid little attention to), so I presume they're all in the user's buffer. A workaround? 'Hate these -- will you still respect me in the morning? (OK: neither do I.) #define BKGND '\0' SRead(f,b,l) char *b; /* read args */ { register int i; register char *c; extern int errno; errno = 0; /* ZERO out the errno flag -- so it can be tested * after SRead() to detect interrupt IYGAD */ /* ZERO out the read buffer: its cheaper if you * know the dirty-bits -- ie., if you only * zap length of soiled bytes. * OK: ZERO is 'NUL-ist' -- AA requires us to * give more-than-equal consideration to * other background chars. */ for ( i=l, c=b; i-- > 0; *c++ = BKGND); /* Only calculate kength when nec. */ if ( (i=read(f,b,l) < 0 ) ) /* Back-Scanning vs. Forward-Scanning: * Wasted effort vs minimum of lost BKGNDs */ for ( c=b+l, i=l+1; --i > 0 && *--c == BKGND; ); return i; } NB: It is a 30-second HACK. It is neither debugged nor fully general. It is far from optimized -- there are conflicting strategies. The IDEA is that you can often infer the LENGTH of data if you can "see" it against a known -- preferably un-receivable -- background. There are multiple attacks and defenses for details of the above implementation. Someone must care about them: Wire Paladin, San Francisco. jcm -- juzz muttering