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

ruby-changes:33902

From: akr <ko1@a...>
Date: Sun, 18 May 2014 09:58:40 +0900 (JST)
Subject: [ruby-changes:33902] akr:r45983 (trunk): * ext/etc/etc.c: Etc.uname method implemented.

akr	2014-05-18 09:58:34 +0900 (Sun, 18 May 2014)

  New Revision: 45983

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

  Log:
    * ext/etc/etc.c: Etc.uname method implemented.
    
    * ext/etc/extconf.rb: Check uname() function.
    
      [ruby-core:62139] [Feature #9770]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/ext/etc/etc.c
    trunk/ext/etc/extconf.rb
    trunk/test/etc/test_etc.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45982)
+++ ChangeLog	(revision 45983)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun May 18 09:58:17 2014  Tanaka Akira  <akr@f...>
+
+	* ext/etc/etc.c: Etc.uname method implemented.
+
+	* ext/etc/extconf.rb: Check uname() function.
+
+	  [ruby-core:62139] [Feature #9770]
+
 Sun May 18 09:16:33 2014  Tanaka Akira  <akr@f...>
 
 	* configure.in: Check nextafter() availability.
Index: ext/etc/extconf.rb
===================================================================
--- ext/etc/extconf.rb	(revision 45982)
+++ ext/etc/extconf.rb	(revision 45983)
@@ -1,6 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ext/etc/extconf.rb#L1
 require 'mkmf'
 
+headers = []
+%w[sys/utsname.h].each {|h|
+  if have_header(h, headers)
+    headers << h
+  end
+}
 have_library("sun", "getpwnam")	# NIS (== YP) interface for IRIX 4
+have_func("uname((struct utsname *)NULL)", headers)
 have_func("getlogin")
 have_func("getpwent")
 have_func("getgrent")
Index: ext/etc/etc.c
===================================================================
--- ext/etc/etc.c	(revision 45982)
+++ ext/etc/etc.c	(revision 45983)
@@ -23,6 +23,10 @@ https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L23
 #include <grp.h>
 #endif
 
+#ifdef HAVE_SYS_UTSNAME_H
+#include <sys/utsname.h>
+#endif
+
 static VALUE sPasswd;
 #ifdef HAVE_GETGRENT
 static VALUE sGroup;
@@ -636,6 +640,50 @@ etc_systmpdir(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L640
     return tmpdir;
 }
 
+#ifdef HAVE_UNAME
+/*
+ * Returns the system information obtained by uname system call.
+ *
+ * The return value is a hash which has 5 keys at least:
+ *   :sysname, :nodename, :release, :version, :machine
+ *
+ * Example:
+ *
+ *   require 'etc'
+ *   require 'pp'
+ *
+ *   pp Etc.uname
+ *   #=> {:sysname=>"Linux",
+ *   #    :nodename=>"boron",
+ *   #    :release=>"2.6.18-6-xen-686",
+ *   #    :version=>"#1 SMP Thu Nov 5 19:54:42 UTC 2009",
+ *   #    :machine=>"i686"}
+ *
+ */
+static VALUE
+etc_uname(VALUE obj)
+{
+    struct utsname u;
+    int ret;
+    VALUE result;
+
+    ret = uname(&u);
+    if (ret == -1)
+        rb_sys_fail("uname");
+
+    result = rb_hash_new();
+    rb_hash_aset(result, ID2SYM(rb_intern("sysname")), rb_str_new_cstr(u.sysname));
+    rb_hash_aset(result, ID2SYM(rb_intern("nodename")), rb_str_new_cstr(u.nodename));
+    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));
+
+    return result;
+}
+#else
+#define etc_uname rb_f_notimplement
+#endif
+
 /*
  * The Etc module provides access to information typically stored in
  * files in the /etc directory on Unix systems.
@@ -685,6 +733,7 @@ Init_etc(void) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L733
     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);
+    rb_define_module_function(mEtc, "uname", etc_uname, 0);
 
     sPasswd =  rb_struct_define_under(mEtc, "Passwd",
 				      "name",
Index: NEWS
===================================================================
--- NEWS	(revision 45982)
+++ NEWS	(revision 45983)
@@ -83,6 +83,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L83
 
 === Stdlib updates (outstanding ones only)
 
+* Etc
+  * New methods:
+    * Etc.uname
+
 * Find, Pathname
   * Extended methods:
     * find method accepts "ignore_error" keyword argument.
Index: test/etc/test_etc.rb
===================================================================
--- test/etc/test_etc.rb	(revision 45982)
+++ test/etc/test_etc.rb	(revision 45983)
@@ -112,4 +112,17 @@ class TestEtc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/etc/test_etc.rb#L112
     Etc.endgrent
     assert_equal(a, b)
   end
+
+  def test_uname
+    begin
+      uname = Etc.uname
+    rescue NotImplementedError
+      return
+    end
+    assert_kind_of(Hash, uname)
+    [:sysname, :nodename, :release, :version, :machine].each {|sym|
+      assert_operator(uname, :has_key?, sym)
+      assert_kind_of(String, uname[sym])
+    }
+  end
 end

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

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