summaryrefslogtreecommitdiff
path: root/ext/etc
diff options
context:
space:
mode:
Diffstat (limited to 'ext/etc')
-rw-r--r--ext/etc/etc.c47
-rw-r--r--ext/etc/extconf.rb4
2 files changed, 50 insertions, 1 deletions
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 <sys/types.h>
#ifdef HAVE_UNISTD_H
@@ -27,6 +28,13 @@ static VALUE sPasswd;
static VALUE sGroup;
#endif
+#ifdef _WIN32
+#include <shlobj.h>
+#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')