home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / TEMPNAME.ICN < prev    next >
Text File  |  1991-09-05  |  2KB  |  56 lines

  1. ############################################################################
  2. #
  3. #    Name:     tempname.icn
  4. #
  5. #    Title:     Get temporary file name
  6. #
  7. #    Author:     Richard L. Goerwitz
  8. #
  9. #    Version: 1.5
  10. #
  11. #    Date:     June 1, 1991
  12. #
  13. ############################################################################
  14. #
  15. #  Need to open up a temporary file?  This procedure prevents you from
  16. #  clobbering existing files by giving you a unique temp file name.
  17. #  Note that tempname() does not return an open file.  It merely returns
  18. #  a string.  The user is responsible for open()'ing a file by that
  19. #  name, and for removing it when done.
  20. #
  21. #  Note that tempname() is a generator, suspending upto 999 unique
  22. #  (and MS-DOS compatible) filenames.
  23. #
  24. #  Bug:  Icon has no exists() call, so the only way we can tell if a
  25. #  filename is already in use is to try to open it for reading.  On
  26. #  most systems, inability to read a file by a given name does not
  27. #  necessarily indicate that the filename is not in use.  Hence this
  28. #  procedure may, under very, very rare circumstances, return the
  29. #  name of a file already in use.  We're safe, though, since if this
  30. #  ever happens to anyone (which I doubt), no files will get clob-
  31. #  bered.  One workaround for the problem is to call tempname() with-
  32. #  out using any intermediate variables, so that it is resumed until
  33. #  some open function succeeds (e.g. open(tempname())).
  34. #  
  35. ############################################################################
  36. #
  37. #  Requires:  UNIX, MS-DOS or another congenial operating system
  38. #
  39. ############################################################################
  40.  
  41. procedure tempname()
  42.  
  43.     static dir
  44.     initial {
  45.     if find("UNIX",&features) then
  46.         dir := "/tmp/"
  47.     else dir := ""
  48.     }
  49.  
  50.     every temp_name := dir || "icontmp." || right(1 to 999,3,"0") do {
  51.     close(open(temp_name)) & next
  52.         suspend \temp_name
  53.     }
  54.  
  55. end
  56.