/*
* Actually read the utmp file, returning an array of hash entries.
*/
static VALUE
cUtmp_read (int argc, VALUE * argv, VALUE self)
{
VALUE result = rb_ary_new ();
VALUE filename;
struct utmp *line;
/*
* Parse the args
*/
rb_scan_args (argc, argv, "01", &filename);
/*
* Set the filename and rewind the pointer
*/
if (TYPE (filename) == T_STRING)
{
#ifdef RSTRING_PTR
utmpname (RSTRING_PTR(filename));
#else
utmpname (RSTRING(filename)->ptr);
#endif
}
else
{
utmpname (_PATH_WTMP);
}
setutent ();
while ((line = getutent ()) != NULL)
{
VALUE entry = rb_hash_new ();
/*
* This is to create IPAddr.new below
*/
VALUE ipaddr_args[1];
rb_hash_aset (entry,
rb_str_new2("user"),
string_or_nil (line->ut_user));
rb_hash_aset (entry,
rb_str_new2("pid"), INT2FIX (line->ut_pid));
// Set a ruby time.
rb_hash_aset (entry,
rb_str_new2("time"),
rb_time_new (line->ut_tv.tv_sec, line->ut_tv.tv_usec));
rb_hash_aset (entry,
rb_str_new2("type"), INT2FIX (line->ut_type));
rb_hash_aset (entry,
rb_str_new2("line"),
string_or_nil (line->ut_line));
rb_hash_aset (entry,
rb_str_new2("host"),
string_or_nil (line->ut_host));
ipaddr_args[0] = get_ip_addr (line->ut_addr_v6);
if (TYPE (ipaddr_args[0]) == T_STRING)
{
rb_hash_aset (entry,
rb_str_new2("ip"),
rb_class_new_instance (1,
ipaddr_args,
rb_const_get (rb_cObject,
rb_intern
("IPAddr"))));
}
else if (TYPE (ipaddr_args[0]) == T_NIL)
{
rb_hash_aset (entry, rb_str_new2("ip"), ipaddr_args[0]);
}
rb_ary_push (result, entry);
}
free (line);
return (result);
}