Date: Fri, 14 Sep 2001 19:17:49 +0300 From: "Eli Zaretskii" Sender: halo1 AT zahav DOT net DOT il To: "Ben Davis" Message-Id: <2950-Fri14Sep2001191748+0300-eliz@is.elta.co.il> X-Mailer: Emacs 20.6 (via feedmail 8.3.emacs20_6 I) and Blat ver 1.8.9 CC: djgpp AT delorie DOT com In-reply-to: <9nsm2c$q0f$1@news5.svr.pol.co.uk> (ben@vjpoole.freeserve.co.uk) Subject: Re: Possible bug in libc tmpnam() function References: <9nsm2c$q0f$1 AT news5 DOT svr DOT pol DOT co DOT uk> Reply-To: djgpp AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk > From: "Ben Davis" > Newsgroups: comp.os.msdos.djgpp > Date: Fri, 14 Sep 2001 11:27:15 +0100 > > I wrote a program which reads a large quantity of data from one file and > then writes data to another. The output file is chosen using the tmpnam() > function, to be renamed at the end. It is most convenient for me to detect > errors by checking if errno is nonzero once at the end of the program. To my > dismay, I found ENOENT in the variable by the time my program had finished - > and this is after the input file had been opened, since an error at that > point would have been output with a different message. As others told you, the value of `errno' should only be tested in the case of failure. And even then, you should zero the value of errno before the call to a library function and test it after the function returns a failure indication. The `tempnam' function is documented to retun NULL if it fails to generate a valid file name. So you should test that instead. > However, it is my understanding that errno should be preserved by > all successful library calls. No, this isn't so. Any system call that fails changes errno, and a successful library function doesn't bother to set errno to the previous value, since no one is supposed to look at errno in that case.