home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / ruby / 1.8 / tmpdir.rb < prev    next >
Encoding:
Ruby Source  |  2005-12-15  |  1.0 KB  |  46 lines

  1. #
  2. # tmpdir - retrieve temporary directory path
  3. #
  4. # $Id: tmpdir.rb,v 1.5.2.1 2005/12/15 15:57:05 matz Exp $
  5. #
  6.  
  7. class Dir
  8.  
  9.   @@systmpdir = '/tmp'
  10.  
  11.   begin
  12.     require 'Win32API'
  13.     max_pathlen = 260
  14.     windir = ' '*(max_pathlen+1)
  15.     begin
  16.       getdir = Win32API.new('kernel32', 'GetSystemWindowsDirectory', 'PL', 'L')
  17.     rescue RuntimeError
  18.       getdir = Win32API.new('kernel32', 'GetWindowsDirectory', 'PL', 'L')
  19.     end
  20.     getdir.call(windir, windir.size)
  21.     windir = File.expand_path(windir.rstrip.untaint)
  22.     temp = File.join(windir, 'temp')
  23.     @@systmpdir = temp if File.directory?(temp) and File.writable?(temp)
  24.   rescue LoadError
  25.   end
  26.  
  27.   ##
  28.   # Returns the operating system's temporary file path.
  29.  
  30.   def Dir::tmpdir
  31.     tmp = '.'
  32.     if $SAFE > 0
  33.       tmp = @@systmpdir
  34.     else
  35.       for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'],
  36.               ENV['USERPROFILE'], @@systmpdir, '/tmp']
  37.     if dir and File.directory?(dir) and File.writable?(dir)
  38.       tmp = dir
  39.       break
  40.     end
  41.       end
  42.     end
  43.     File.expand_path(tmp)
  44.   end
  45. end
  46.