2 class Passwd < FFI::Struct
5 s.include "sys/types.h"
9 s.field :pw_name, :string
10 s.field :pw_passwd, :string
11 s.field :pw_uid, :uint
12 s.field :pw_gid, :uint
13 s.field :pw_dir, :string
14 s.field :pw_shell, :string
18 def name; self[:pw_name]; end
19 def passwd; self[:pw_passwd]; end
20 def uid; self[:pw_uid]; end
21 def gid; self[:pw_gid]; end
22 def dir; self[:pw_dir]; end
23 def shell; self[:pw_shell]; end
26 class Group < FFI::Struct
29 s.include "sys/types.h"
33 s.field :gr_name, :string
34 s.field :gr_gid, :uint
38 def name; self[:gr_name]; end
39 def gid; self[:gr_gid]; end
46 def self.getpwnam(name)
47 login = StringValue(name)
49 passwd_ptr = Platform::POSIX.getpwnam(name)
51 raise ArgumentError, "cannot find user - #{name}"
54 Passwd.new(passwd_ptr)
57 def self.getpwuid(uid = Process.uid)
58 uid = Type.coerce_to(uid, Integer, :to_int)
60 passwd_ptr = Platform::POSIX.getpwuid(uid)
62 raise ArgumentError, "cannot find user - #{uid}"
65 Passwd.new(passwd_ptr)
69 Platform::POSIX.setpwent
73 passwd_ptr = Platform::POSIX.getpwent
74 return nil if passwd_ptr.nil?
76 Passwd.new(passwd_ptr)
80 Platform::POSIX.endpwent
96 def self.getgrnam(name)
97 name = StringValue(name)
99 group_ptr = Platform::POSIX.getgrnam(name)
101 raise ArgumentError, "cannot find group - #{name}"
107 def self.getgrgid(gid = Process.gid)
108 gid = Type.coerce_to(gid, Integer, :to_int)
110 group_ptr = Platform::POSIX.getgrgid(gid)
112 raise ArgumentError, "cannot find group - #{gid}"
119 Platform::POSIX.setgrent
123 group_ptr = Platform::POSIX.getgrent
124 return nil if group_ptr.nil?
130 Platform::POSIX.endgrent