From: WATANABE Hirofumi Date: 2003-07-23T17:54:14+09:00 Subject: [ruby-dev:20835] Re: [ruby-cvs] ruby/lib: * lib/tmpdir.rb: new library to get temporary directory path, わたなべです。 Yukihiro Matsumoto writes: > |GetTempPathは [ruby-talk:76524] によると > | > |## 1. The path specified by the TMP environment variable. > |## 2. The path specified by the TEMP environment variable, > | if TMP is not defined. > |## 3. The current directory, if both TMP and TEMP are not defined. > | > |ということみたいですが、これでは$SAFEに関係なくWIN32APIが使 > |える処理系では結局環境変数を参照することになります。 > | > |それでいいのですか? > > 私にはマイクロソフトがそれで良いのだといっているように読めま > した。実際のところどうしたもんでしょうねえ。 http://www.microsoft.com/japan/developer/library/jpwinpf/_win32_gettemppath.htm を見るとなぜか、NT系は手抜きになってるようです。 しかもC:\Windowsというのはかなりうれしくない仕様です。 いろいろ調べた結果$WINDIR/tempがよさそうなので後述のようにし ようと思ったんですが、requireは$SAFE=0で実行されます。よって ここで$SAFEを見ても無意味です。 どうするのがいいんだろう? # # tmpdir - retrieve temporary directory path # # $Id: tmpdir.rb,v 1.3 2003/07/22 08:42:47 matz Exp $ # class Dir begin require "Win32API" max_pathlen = 260 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 = File.directory?(temp) && File.writable?(temp) ? temp : nil rescue LoadError end if $SAFE > 0 TMPDIR = '/tmp' else for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], systmpdir, '/tmp'] if dir and File.directory?(dir) and File.writable?(dir) TMPDIR = File.expand_path(dir) break end end TMPDIR = '.' unless defined? TMPDIR end end if __FILE__ == $0 puts Dir::TMPDIR end