home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / sgi / 18266 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  2.6 KB

  1. Xref: sparky comp.sys.sgi:18266 comp.sys.sgi.bugs:32 comp.unix.ultrix:9034
  2. Newsgroups: comp.sys.sgi,comp.sys.sgi.bugs,comp.unix.ultrix
  3. 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
  4. From: kinch@valve.heart.rri.uwo.ca (Dave Kinchlea)
  5. Subject: Re: tmpnam/tempnam 
  6. Organization: University of Western Ontario, London, Ont. CA
  7. Date: Tue, 22 Dec 1992 14:31:24 GMT
  8. Message-ID: <1992Dec22.143124.7110@julian.uwo.ca>
  9. References:  <BzKIrq.GqI@world.std.com>
  10. Sender: news@julian.uwo.ca (USENET News System)
  11. Nntp-Posting-Host: valve.heart.rri.uwo.ca
  12. Lines: 53
  13.  
  14. In article <BzKIrq.GqI@world.std.com>, cmr@world.std.com (Charles M Richmond) writes:
  15. |> 
  16. |>   All of this discussion about tmpnam/tempnam has lead to small bits of
  17. |> foolishness on the part of a lot of people, myself not excepted.
  18. |> 
  19. |> Lets discuss the proper use of tmpnam/tempnam.  After creating the filename
  20. |> one should open the file as quickly as possible , inorder to preclude the
  21. |> creation of an a same named file by another process. Naturally enough one
  22. |> would also want to verify the uniqueness of the file name at the start. The
  23. |> tmpnam/tempnam duo handles this quite nicely by testing for you. The proper 
  24. |> sequence of code would then be thus:
  25. |> 
  26. |> 
  27. |> fname = tmpnam(arg);
  28. |> if (errno == ENOENT)
  29. |>    {
  30. |>    open(.....
  31. |> 
  32. |> You can massage this in many ways.  As an example one could add on a while
  33. |> loop for errno != ENOENT to continuously try new names until a unique one
  34. |> is found.
  35. |> 
  36. |> Charlie
  37. |> 
  38. |> -- 
  39. |> *****************************************************************************
  40. |> *  Charles Richmond     Integrated International Systems Corporation        *
  41. |> *  cmr@world.std.com    (Temporary) cmr@koneko.shr.dec.com            *
  42. |> *  Specializing in UNIX, X, Image Processing, and Communications.           *
  43.  
  44. UGG, this is NOT the `proper sequence of code' at all. As others have pointed out,
  45. testing errno in this fashion is WRONG! This is closer:
  46.  
  47. if ((fname = tmpnam(arg)) != NULL) {
  48.     if (( filedes = open(fname,O_SomeWriteMode)) == -1) {
  49.         /* Now check errno to your hearts content */
  50.         
  51.     }
  52. } else {
  53.     /* Here (and only here) it is valid to check errno for the tmpnam call */
  54. }
  55.  
  56. You can argue style if you like but the point is that errno is not tested
  57. until it is known that an error has occured. 
  58. Just because ENOENT was probably generated internally in tmpnam() doesn't
  59. mean that it will be that after the call. It would be perfectly reasonable
  60. and legal behaviour to clear errno (or set it to a completely bogus value)
  61. if the call succeeds.
  62.  
  63.  
  64. Just trying to keep the misinformation down to a minimum.
  65.  
  66. cheerskinch
  67.