Module: Etc

Defined in:
lib/etc.rb

Constant Summary collapse

FFI =
Rubinius::FFI

Class Method Summary collapse

Class Method Details

.endgrentObject



156
157
158
# File 'lib/etc.rb', line 156

def self.endgrent
  FFI::Platform::POSIX.endgrent
end

.endpwentObject



102
103
104
# File 'lib/etc.rb', line 102

def self.endpwent
  FFI::Platform::POSIX.endpwent
end

.getgrentObject



149
150
151
152
153
154
# File 'lib/etc.rb', line 149

def self.getgrent
  group_ptr = FFI::Platform::POSIX.getgrent
  return nil if group_ptr.nil?

  Struct::Group.new(group_ptr)
end

.getgrgid(gid = Process.gid) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/etc.rb', line 134

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

.getgrnam(name) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/etc.rb', line 123

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

.getloginObject



65
66
67
# File 'lib/etc.rb', line 65

def self.getlogin
  getpwuid.name
end

.getpwentObject



95
96
97
98
99
100
# File 'lib/etc.rb', line 95

def self.getpwent
  passwd_ptr = FFI::Platform::POSIX.getpwent
  return nil if passwd_ptr.nil?

  Struct::Passwd.new(passwd_ptr)
end

.getpwnam(name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/etc.rb', line 69

def self.getpwnam(name)
   = 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

.getpwuid(uid = Process.uid) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/etc.rb', line 80

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

.groupObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/etc.rb', line 160

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

.passwdObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/etc.rb', line 106

def self.passwd
  Rubinius.synchronize(self) do
    begin
      setpwent

      loop do
        pw = getpwent
        return if pw.nil?

        yield pw
      end
    ensure
      endpwent
    end
  end
end

.setgrentObject



145
146
147
# File 'lib/etc.rb', line 145

def self.setgrent
  FFI::Platform::POSIX.setgrent
end

.setpwentObject



91
92
93
# File 'lib/etc.rb', line 91

def self.setpwent
  FFI::Platform::POSIX.setpwent
end