[前][次][番号順一覧][スレッド一覧]

ruby-changes:33933

From: nobu <ko1@a...>
Date: Mon, 19 May 2014 16:54:38 +0900 (JST)
Subject: [ruby-changes:33933] nobu:r46014 (trunk): etc.c: Etc.uname on Windows

nobu	2014-05-19 16:54:25 +0900 (Mon, 19 May 2014)

  New Revision: 46014

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=46014

  Log:
    etc.c: Etc.uname on Windows
    
    * ext/etc/etc.c (etc_uname): add support for Windows using
      GetVersionExW(), GetSystemInfo(), and GetComputerNameExW() with
      `ComputerNameDnsHostname`.  [Feature #9842]

  Modified files:
    trunk/ChangeLog
    trunk/ext/etc/etc.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46013)
+++ ChangeLog	(revision 46014)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon May 19 16:54:22 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/etc/etc.c (etc_uname): add support for Windows using
+	  GetVersionExW(), GetSystemInfo(), and GetComputerNameExW() with
+	  `ComputerNameDnsHostname`.  [Feature #9842]
+
 Mon May 19 16:29:48 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* string.c (rb_pat_search): advance by byte offset but not by char
Index: ext/etc/etc.c
===================================================================
--- ext/etc/etc.c	(revision 46013)
+++ ext/etc/etc.c	(revision 46014)
@@ -40,6 +40,7 @@ static VALUE sGroup; https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L40
 #ifndef CSIDL_COMMON_APPDATA
 #define CSIDL_COMMON_APPDATA 35
 #endif
+#define HAVE_UNAME 1
 #endif
 
 #ifndef _WIN32
@@ -668,6 +669,69 @@ etc_systmpdir(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L669
 static VALUE
 etc_uname(VALUE obj)
 {
+#ifdef _WIN32
+    OSVERSIONINFOW v;
+    SYSTEM_INFO s;
+    const char *sysname, *mach;
+    VALUE result, release, version;
+    VALUE vbuf, nodename = Qnil;
+    DWORD len = 0;
+    WCHAR *buf;
+
+    v.dwOSVersionInfoSize = sizeof(v);
+    if (!GetVersionExW(&v))
+        rb_sys_fail("GetVersionEx");
+
+    result = rb_hash_new();
+    switch (v.dwPlatformId) {
+      case VER_PLATFORM_WIN32s:
+	sysname = "Win32s";
+	break;
+      case VER_PLATFORM_WIN32_NT:
+	sysname = "Windows_NT";
+	break;
+      case VER_PLATFORM_WIN32_WINDOWS:
+      default:
+	sysname = "Windows";
+	break;
+    }
+    rb_hash_aset(result, ID2SYM(rb_intern("sysname")), rb_str_new_cstr(sysname));
+    release = rb_sprintf("%lu.%lu.%lu", v.dwMajorVersion, v.dwMinorVersion, v.dwBuildNumber);
+    rb_hash_aset(result, ID2SYM(rb_intern("release")), release);
+    version = rb_sprintf("%s Version %"PRIsVALUE": %"PRIsVALUE, sysname, release,
+			 rb_w32_conv_from_wchar(v.szCSDVersion, rb_utf8_encoding()));
+    rb_hash_aset(result, ID2SYM(rb_intern("version")), version);
+
+    GetComputerNameExW(ComputerNameDnsFullyQualified, NULL, &len);
+    buf = ALLOCV_N(WCHAR, vbuf, len);
+    if (GetComputerNameExW(ComputerNameDnsHostname, buf, &len)) {
+	nodename = rb_w32_conv_from_wchar(buf, rb_utf8_encoding());
+    }
+    ALLOCV_END(vbuf);
+    if (NIL_P(nodename)) nodename = rb_str_new(0, 0);
+    rb_hash_aset(result, ID2SYM(rb_intern("nodename")), nodename);
+
+    GetSystemInfo(&s);
+    switch (s.wProcessorArchitecture) {
+      case PROCESSOR_ARCHITECTURE_AMD64:
+	mach = "x64";
+	break;
+      case PROCESSOR_ARCHITECTURE_ARM:
+	mach = "ARM";
+	break;
+      case PROCESSOR_ARCHITECTURE_IA64:
+	mach = "IA64";
+	break;
+      case PROCESSOR_ARCHITECTURE_INTEL:
+	mach = "x86";
+	break;
+      default:
+	mach = "unknown";
+	break;
+    }
+
+    rb_hash_aset(result, ID2SYM(rb_intern("machine")), rb_str_new_cstr(mach));
+#else
     struct utsname u;
     int ret;
     VALUE result;
@@ -682,6 +746,7 @@ etc_uname(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L746
     rb_hash_aset(result, ID2SYM(rb_intern("release")), rb_str_new_cstr(u.release));
     rb_hash_aset(result, ID2SYM(rb_intern("version")), rb_str_new_cstr(u.version));
     rb_hash_aset(result, ID2SYM(rb_intern("machine")), rb_str_new_cstr(u.machine));
+#endif
 
     return result;
 }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]