Checking return values (was: Trojan Horses, NFS, etc.)

Stanley Friesen sarima at tdatirv.UUCP
Sat Oct 27 00:57:51 AEST 1990


In article <1990Oct25.075856.4923 at robobar.co.uk> ronald at robobar.co.uk (Ronald S H Khoo) writes:
>scs at adam.mit.edu (Steve Summit) writes:
>>			default:
>>				fprintf(stderr, "%s: can't open %s: ",
>>							progname, name);
>>				perror("");
>For those of you still puzzled: perror() must be called *immediately* after the
>error returns, or the internal error state may be modified by calls
>to system calls in the intervening situation.

My solution, when I feel I *really* must place something in between the error
and the call to perror(), is to save and restore errno.  Thus:

	default:
		old_err = errno;
		fprintf(stderr, "%s: can't open %s: ", progname, name);
		errno = old_err;
		perror("");


Actually, I am likely to make the fprintf() above into an sprintf and pass
the resulting string to perror() - it gets the same results, and doesn't
involve passing an empty string to perror().

had enough?

-- 
---------------
uunet!tdatirv!sarima				(Stanley Friesen)



More information about the Comp.unix.programmer mailing list