home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.sys.sgi:18266 comp.sys.sgi.bugs:32 comp.unix.ultrix:9034
- Newsgroups: comp.sys.sgi,comp.sys.sgi.bugs,comp.unix.ultrix
- Path: sparky!uunet!spool.mu.edu!yale.edu!qt.cs.utexas.edu!cs.utexas.edu!torn!newshost.uwo.ca!valve.heart.rri.uwo.ca!kinch
- From: kinch@valve.heart.rri.uwo.ca (Dave Kinchlea)
- Subject: Re: tmpnam/tempnam
- Organization: University of Western Ontario, London, Ont. CA
- Date: Tue, 22 Dec 1992 14:31:24 GMT
- Message-ID: <1992Dec22.143124.7110@julian.uwo.ca>
- References: <BzKIrq.GqI@world.std.com>
- Sender: news@julian.uwo.ca (USENET News System)
- Nntp-Posting-Host: valve.heart.rri.uwo.ca
- Lines: 53
-
- In article <BzKIrq.GqI@world.std.com>, cmr@world.std.com (Charles M Richmond) writes:
- |>
- |> All of this discussion about tmpnam/tempnam has lead to small bits of
- |> foolishness on the part of a lot of people, myself not excepted.
- |>
- |> Lets discuss the proper use of tmpnam/tempnam. After creating the filename
- |> one should open the file as quickly as possible , inorder to preclude the
- |> creation of an a same named file by another process. Naturally enough one
- |> would also want to verify the uniqueness of the file name at the start. The
- |> tmpnam/tempnam duo handles this quite nicely by testing for you. The proper
- |> sequence of code would then be thus:
- |>
- |>
- |> fname = tmpnam(arg);
- |> if (errno == ENOENT)
- |> {
- |> open(.....
- |>
- |> You can massage this in many ways. As an example one could add on a while
- |> loop for errno != ENOENT to continuously try new names until a unique one
- |> is found.
- |>
- |> Charlie
- |>
- |> --
- |> *****************************************************************************
- |> * Charles Richmond Integrated International Systems Corporation *
- |> * cmr@world.std.com (Temporary) cmr@koneko.shr.dec.com *
- |> * Specializing in UNIX, X, Image Processing, and Communications. *
-
- UGG, this is NOT the `proper sequence of code' at all. As others have pointed out,
- testing errno in this fashion is WRONG! This is closer:
-
- if ((fname = tmpnam(arg)) != NULL) {
- if (( filedes = open(fname,O_SomeWriteMode)) == -1) {
- /* Now check errno to your hearts content */
-
- }
- } else {
- /* Here (and only here) it is valid to check errno for the tmpnam call */
- }
-
- You can argue style if you like but the point is that errno is not tested
- until it is known that an error has occured.
- Just because ENOENT was probably generated internally in tmpnam() doesn't
- mean that it will be that after the call. It would be perfectly reasonable
- and legal behaviour to clear errno (or set it to a completely bogus value)
- if the call succeeds.
-
-
- Just trying to keep the misinformation down to a minimum.
-
- cheerskinch
-