aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tmpdir.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tmpdir.rb')
-rw-r--r--lib/tmpdir.rb38
1 files changed, 27 insertions, 11 deletions
diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb
index 6fde398ed4..eaec830b08 100644
--- a/lib/tmpdir.rb
+++ b/lib/tmpdir.rb
@@ -5,22 +5,38 @@
#
class Dir
+
+ @@systmpdir = '/tmp'
+
begin
- require "Win32API"
+ require 'Win32API'
max_pathlen = 260
- t_path = ' '*(max_pathlen+1)
- t_path = t_path[0, Win32API.new('kernel32', 'GetTempPath', 'LP', 'L').call(t_path.size, t_path)]
- t_path.untaint
- TMPDIR = File.expand_path(t_path)
+ windir = ' '*(max_pathlen+1)
+ begin
+ getdir = Win32API.new('kernel32', 'GetSystemWindowsDirectory', 'PL', 'L')
+ rescue RuntimeError
+ getdir = Win32API.new('kernel32', 'GetSystemDirectory', 'PL', 'L')
+ end
+ getdir.call(windir, windir.size)
+ windir = File.expand_path(windir.rstrip.untaint)
+ temp = File.join(windir, 'temp')
+ @@systmpdir = temp if File.directory?(temp) and File.writable?(temp)
rescue LoadError
+ end
+
+ def Dir::tmpdir
+ tmp = '.'
if $SAFE > 0
- TMPDIR = '/tmp'
+ tmp = @@systmpdir
else
- TMPDIR = File.expand_path(ENV['TMPDIR']||ENV['TMP']||ENV['TEMP']||'/tmp')
+ for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'],
+ ENV['USERPROFILE'], @@systmpdir, '/tmp']
+ if dir and File.directory?(dir) and File.writable?(dir)
+ tmp = dir
+ break
+ end
+ end
end
+ File.expand_path(tmp)
end
end
-
-if __FILE__ == $0
- puts Dir::TMPDIR
-end