[#4479] Requesting addition to IRB (configurable standard output) — Sascha Ebach <se@...>

Hello,

13 messages 2005/02/24
[#4482] Re: Requesting addition to IRB (configurable standard output) — Sam Roberts <sroberts@...> 2005/02/25

Quoting se@digitale-wertschoepfung.de, on Fri, Feb 25, 2005 at 01:22:34AM +0900:

[#4483] Re: Requesting addition to IRB (configurable standard output) — Eric Hodel <drbrain@...7.net> 2005/02/25

On 24 Feb 2005, at 19:51, Sam Roberts wrote:

[#4488] Re: Requesting addition to IRB (configurable standard output) — Sam Roberts <sroberts@...> 2005/02/26

Quoting drbrain@segment7.net, on Sat, Feb 26, 2005 at 02:43:31AM +0900:

[#4489] Re: Requesting addition to IRB (configurable standard output) — Eric Hodel <drbrain@...7.net> 2005/02/26

On 25 Feb 2005, at 16:03, Sam Roberts wrote:

Re: Adding lastlog info to etc

From: nobu.nokada@...
Date: 2005-02-06 13:45:37 UTC
List: ruby-core #4372
Hi,

At Sat, 5 Feb 2005 22:02:13 +0900,
Daniel Berger wrote in [ruby-core:04363]:
> > > Any chance of adding lastlog information to the etc module?  I have a
> > > patch, but it only works for Solaris at the moment.  Basically, it means
> > > altering the extconf.rb file and doing the following:
> > 
> > Do you think it should be included in Passwd struct?
> > 
> > -- 
> > Nobu Nakada
> 
> Yes.

I can't agree.

1) Passwd can't contain all per-user information.

2) lastlog is a per-uid information but not per-logname like as
   Passwd.

My proposal is to add Etc.lastlog method.


Index: ext/etc/etc.c
===================================================================
RCS file: /cvs/ruby/src/ruby/ext/etc/etc.c,v
retrieving revision 1.16
diff -U2 -p -r1.16 etc.c
--- ext/etc/etc.c	24 Nov 2003 10:42:17 -0000	1.16
+++ ext/etc/etc.c	6 Feb 2005 04:08:06 -0000
@@ -24,4 +24,19 @@
 #endif
 
+#ifdef HAVE_LASTLOG_H
+#include <lastlog.h>
+#include <fcntl.h>
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+# ifndef _PATH_LASTLOG
+#  define _PATH_LASTLOG "/var/adm/lastlog"
+# endif
+#endif
+#endif
+
+#ifndef HAVE_TYPE_UID_T
+#define uid_t int
+#endif
+
 static VALUE sPasswd, sGroup;
 
@@ -360,4 +375,70 @@ etc_getgrent(obj)
 }
 
+#ifdef HAVE_LASTLOG_H
+static struct lastlog *
+getlastlog(uid, ll_entry)
+    uid_t uid;
+    struct lastlog *ll_entry;
+{
+    int fd;
+    unsigned int size;
+    off_t ofs;
+
+    if ((fd = open(_PATH_LASTLOG, O_RDONLY)) == -1) {
+      failed:
+	rb_sys_fail(_PATH_LASTLOG);
+    }
+
+    ofs = uid * (off_t)sizeof(*ll_entry);
+#ifdef HAVE_PREAD
+    if (pread(fd, ll_entry, sizeof(*ll_entry), ofs) != sizeof(*ll_entry))
+	goto failed;
+#else
+    if (lseek(fd, ofs, SEEK_SET) < 0)
+	goto failed;
+    if (read(fd, ll_entry, sizeof(*ll_entry)) != sizeof(*ll_entry))
+	goto failed;
+#endif
+    close(fd);
+
+    return ll_entry;
+}
+
+static VALUE sLastLog;
+
+static VALUE
+etc_lastlog(self, user)
+    VALUE self, user;
+{
+    VALUE nam;
+    uid_t uid;
+    struct lastlog ll_entry;
+    time_t time;
+
+    rb_secure(4);
+    nam = rb_check_string_type(user);
+    if (!NIL_P(nam)) {
+	struct passwd* pwd;
+	rb_check_safe_obj(nam);
+	pwd = getpwnam(RSTRING(nam)->ptr);
+	if (pwd == 0)
+	    rb_raise(rb_eArgError, "can't find user for %s", RSTRING(nam)->ptr);
+	uid = pwd->pw_uid;
+    }
+    else {
+	uid = NUM2INT(user);
+    }
+
+    if (!getlastlog(uid, &ll_entry))
+
+    time = ll_entry.ll_time;
+    return rb_struct_new(sLastLog,
+			 time ? rb_time_new(time, 0) : Qnil,
+			 safe_setup_str(ll_entry.ll_line),
+			 safe_setup_str(ll_entry.ll_host),
+			 (VALUE)0);
+}
+#endif
+
 static VALUE mEtc;
 
@@ -414,3 +495,9 @@ Init_etc()
     rb_global_variable(&sGroup);
 #endif
+
+#ifdef HAVE_LASTLOG_H
+    rb_define_module_function(mEtc, "lastlog", etc_lastlog, 1);
+    sLastLog = rb_struct_define("LastLog", "time", "line", "host", NULL);
+    rb_global_variable(&sLastLog);
+#endif
 }
Index: ext/etc/extconf.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/ext/etc/extconf.rb,v
retrieving revision 1.5
diff -U2 -p -r1.5 extconf.rb
--- ext/etc/extconf.rb	24 Nov 2003 10:42:17 -0000	1.5
+++ ext/etc/extconf.rb	6 Feb 2005 04:13:35 -0000
@@ -15,4 +15,6 @@ if  a or b or c
   have_struct_member('struct passwd', 'pw_passwd', 'pwd.h')
   have_struct_member('struct group', 'gr_passwd', 'grp.h')
+  have_header("lastlog.h") and have_header("paths.h")
+  have_type("uid_t");
   create_makefile("etc")
 end


-- 
Nobu Nakada

In This Thread