Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

157 lines (122 sloc) 2.998 kb
class Struct::Passwd < FFI::Struct
@@@
struct do |s|
s.include "sys/types.h"
s.include "pwd.h"
s.name "struct passwd"
s.field :pw_name, :string
s.field :pw_passwd, :string
s.field :pw_uid, :uid_t
s.field :pw_gid, :gid_t
s.field :pw_dir, :string
s.field :pw_shell, :string
end
@@@
def name; self[:pw_name]; end
def passwd; self[:pw_passwd]; end
def uid; self[:pw_uid]; end
def gid; self[:pw_gid]; end
def dir; self[:pw_dir]; end
def shell; self[:pw_shell]; end
end
class Struct::Group < FFI::Struct
@@@
struct do |s|
s.include "sys/types.h"
s.include "grp.h"
s.name "struct group"
s.field :gr_name, :string
s.field :gr_gid, :gid_t
end
@@@
def name; self[:gr_name]; end
def gid; self[:gr_gid]; end
end
module Etc
const_set(:Passwd, Struct::Passwd)
const_set(:Group, Struct::Group)
def self.getlogin
getpwuid.name
end
def self.getpwnam(name)
login = StringValue(name)
passwd_ptr = FFI::Platform::POSIX.getpwnam(name)
if passwd_ptr.nil?
raise ArgumentError, "cannot find user - #{name}"
end
Struct::Passwd.new(passwd_ptr)
end
def self.getpwuid(uid = Process.uid)
uid = Rubinius::Type.coerce_to(uid, Integer, :to_int)
passwd_ptr = FFI::Platform::POSIX.getpwuid(uid)
if passwd_ptr.nil?
raise ArgumentError, "cannot find user - #{uid}"
end
Struct::Passwd.new(passwd_ptr)
end
def self.setpwent
FFI::Platform::POSIX.setpwent
end
def self.getpwent
passwd_ptr = FFI::Platform::POSIX.getpwent
return nil if passwd_ptr.nil?
Struct::Passwd.new(passwd_ptr)
end
def self.endpwent
FFI::Platform::POSIX.endpwent
end
def self.passwd
setpwent
loop do
pw = getpwent
return if pw.nil?
yield pw
end
ensure
endpwent
end
def self.getgrnam(name)
name = StringValue(name)
group_ptr = FFI::Platform::POSIX.getgrnam(name)
if group_ptr.nil?
raise ArgumentError, "cannot find group - #{name}"
end
Struct::Group.new(group_ptr)
end
def self.getgrgid(gid = Process.gid)
gid = Rubinius::Type.coerce_to(gid, Integer, :to_int)
group_ptr = FFI::Platform::POSIX.getgrgid(gid)
if group_ptr.nil?
raise ArgumentError, "cannot find group - #{gid}"
end
Struct::Group.new(group_ptr)
end
def self.setgrent
FFI::Platform::POSIX.setgrent
end
def self.getgrent
group_ptr = FFI::Platform::POSIX.getgrent
return nil if group_ptr.nil?
Struct::Group.new(group_ptr)
end
def self.endgrent
FFI::Platform::POSIX.endgrent
end
def self.group
Rubinius.synchronize(self) do
begin
raise "parallel group iteration" if @parallel_iteration
@parallel_iteration = true
setgrent
loop do
gr = getgrent
return if gr.nil?
yield gr
end
ensure
endgrent
@parallel_iteration = false
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.