diff --git a/ChangeLog b/ChangeLog index c472c3b5fd..2b39d3e005 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Sat May 8 12:25:15 2010 Nobuyoshi Nakada + + * ext/etc/etc.c (etc_systmpdir): moved from ext/tmpdir. + + * ext/etc/etc.c (etc_sysconfdir): added. + + * lib/rubygems/config_file.rb, lib/tmpdir.rb: use etc. + Sat May 8 11:07:41 2010 Nobuyoshi Nakada * ext/bigdecimal/bigdecimal.c (VpAlloc): ensure buf does not get diff --git a/ext/etc/etc.c b/ext/etc/etc.c index 79462f700c..0a01acf47d 100644 --- a/ext/etc/etc.c +++ b/ext/etc/etc.c @@ -8,6 +8,7 @@ ************************************************/ #include "ruby.h" +#include "ruby/encoding.h" #include #ifdef HAVE_UNISTD_H @@ -27,6 +28,13 @@ static VALUE sPasswd; static VALUE sGroup; #endif +#ifdef _WIN32 +#include +#ifndef CSIDL_COMMON_APPDATA +#define CSIDL_COMMON_APPDATA 35 +#endif +#endif + #ifndef _WIN32 char *getenv(); #endif @@ -549,6 +557,43 @@ etc_getgrent(VALUE obj) return Qnil; } +#define numberof(array) (sizeof(array) / sizeof(*array)) + +#ifdef _WIN32 +VALUE rb_w32_special_folder(int type); +UINT rb_w32_system_tmpdir(WCHAR *path, UINT len); +VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc); +#endif + +/* + * Returns system configuration directory. + */ +static VALUE +etc_sysconfdir(VALUE obj) +{ +#ifdef _WIN32 + return rb_w32_special_folder(CSIDL_COMMON_APPDATA); +#else + return rb_filesystem_str_new_cstr(SYSCONFDIR); +#endif +} + +/* + * Returns system temporary directory. + */ +static VALUE +etc_systmpdir(void) +{ +#ifdef _WIN32 + WCHAR path[_MAX_PATH]; + UINT len = rb_w32_system_tmpdir(path, numberof(path)); + if (!len) return Qnil; + return rb_w32_conv_from_wchar(path, rb_filesystem_encoding()); +#else + return rb_filesystem_str_new_cstr("/tmp"); +#endif +} + /* * The etc module provides access to information from the running OS. * @@ -575,6 +620,8 @@ Init_etc(void) rb_define_module_function(mEtc, "setgrent", etc_setgrent, 0); rb_define_module_function(mEtc, "endgrent", etc_endgrent, 0); rb_define_module_function(mEtc, "getgrent", etc_getgrent, 0); + rb_define_module_function(mEtc, "sysconfdir", etc_sysconfdir, 0); + rb_define_module_function(mEtc, "systmpdir", etc_systmpdir, 0); sPasswd = rb_struct_define("Passwd", "name", "passwd", "uid", "gid", diff --git a/ext/etc/extconf.rb b/ext/etc/extconf.rb index 2914bfb196..7293d7b805 100644 --- a/ext/etc/extconf.rb +++ b/ext/etc/extconf.rb @@ -4,7 +4,9 @@ have_library("sun", "getpwnam") # NIS (== YP) interface for IRIX 4 a = have_func("getlogin") b = have_func("getpwent") c = have_func("getgrent") -if a or b or c +sysconfdir = RbConfig.expand(RbConfig::MAKEFILE_CONFIG["sysconfdir"].dup, "prefix"=>"") +$defs.push("-DSYSCONFDIR=#{Shellwords.escape(sysconfdir.dump)}") +if a or b or c or sysconfdir have_struct_member('struct passwd', 'pw_gecos', 'pwd.h') have_struct_member('struct passwd', 'pw_change', 'pwd.h') have_struct_member('struct passwd', 'pw_quota', 'pwd.h') diff --git a/ext/tmpdir/extconf.rb b/ext/tmpdir/extconf.rb deleted file mode 100644 index a283f851b0..0000000000 --- a/ext/tmpdir/extconf.rb +++ /dev/null @@ -1,6 +0,0 @@ -case -when have_func("rb_w32_system_tmpdir") - ok = true # win32 -else -end -create_makefile("tmpdir") if ok diff --git a/ext/tmpdir/tmpdir.c b/ext/tmpdir/tmpdir.c deleted file mode 100644 index f046713b63..0000000000 --- a/ext/tmpdir/tmpdir.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include - -#define numberof(array) (sizeof(array) / sizeof(*array)) - -#ifdef HAVE_RB_W32_SYSTEM_TMPDIR -UINT rb_w32_system_tmpdir(WCHAR *path, UINT len); -VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc); -#endif - -static VALUE -system_tmpdir(void) -{ -#ifdef HAVE_RB_W32_SYSTEM_TMPDIR - WCHAR path[_MAX_PATH]; - UINT len = rb_w32_system_tmpdir(path, numberof(path)); - if (!len) return Qnil; - return rb_w32_conv_from_wchar(path, rb_filesystem_encoding()); -#else - return rb_filesystem_str_new_cstr("/tmp"); -#endif -} - -/* - * sets Dir.@@systmpdir. - */ -void -Init_tmpdir(void) -{ - rb_cvar_set(rb_cDir, rb_intern_const("@@systmpdir"), - rb_obj_freeze(system_tmpdir())); -} diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb index 3498d06540..1a96356fa8 100644 --- a/lib/rubygems/config_file.rb +++ b/lib/rubygems/config_file.rb @@ -47,15 +47,8 @@ class Gem::ConfigFile system_config_path = begin - require 'Win32API' - - CSIDL_COMMON_APPDATA = 0x0023 - path = 0.chr * 260 - SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'PLPLP', 'L', - :stdcall - SHGetFolderPath.call nil, CSIDL_COMMON_APPDATA, nil, 1, path - - path.strip + require 'etc.so' + Etc.sysconfdir rescue LoadError '/etc' end diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb index 2f9a2486b0..f043ad5006 100644 --- a/lib/tmpdir.rb +++ b/lib/tmpdir.rb @@ -5,13 +5,14 @@ # require 'fileutils' -if /mswin|mingw/ =~ RUBY_PLATFORM - require 'tmpdir.so' +begin + require 'etc.so' +rescue LoadError end class Dir - @@systmpdir ||= '/tmp' + @@systmpdir ||= defined?(Etc.systmpdir) ? Etc.systmpdir : '/tmp' ## # Returns the operating system's temporary file path. diff --git a/win32/win32.c b/win32/win32.c index 5b05510209..3b935b1d36 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -68,6 +68,7 @@ static int has_redirection(const char *); int rb_w32_wait_events(HANDLE *events, int num, DWORD timeout); static int rb_w32_open_osfhandle(intptr_t osfhandle, int flags); static int wstati64(const WCHAR *path, struct stati64 *st); +VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc); #define RUBY_CRITICAL(expr) do { expr; } while (0) @@ -466,6 +467,16 @@ get_system_directory(WCHAR *path, UINT len) #define numberof(array) (sizeof(array) / sizeof(*array)) +VALUE +rb_w32_special_folder(int type) +{ + WCHAR path[_MAX_PATH]; + + if (!get_special_folder(type, path)) return Qnil; + regulate_path(path); + return rb_w32_conv_from_wchar(path, rb_filesystem_encoding()); +} + UINT rb_w32_system_tmpdir(WCHAR *path, UINT len) {