/
env.rb
256 lines (193 loc) · 4.52 KB
/
env.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- encoding: us-ascii -*-
##
# Interface to process environment variables.
module Rubinius
class EnvironmentVariables
include Enumerable
include Rubinius::EnvironmentAccess
def [](key)
value = getenv(StringValue(key))
set_encoding value
value.freeze
end
def []=(key, value)
key = StringValue(key)
if value.nil?
unsetenv(key)
else
setenv key, StringValue(value), 1
end
value
end
alias_method :store, :[]=
def each_key
return to_enum(:each_key) unless block_given?
each { |k, v| yield k }
end
def each_value
return to_enum(:each_value) unless block_given?
each { |k, v| yield v }
end
def each
return to_enum(:each) unless block_given?
env = environ()
ptr_size = FFI.type_size FFI.find_type(:pointer)
offset = 0
cur = env + offset
until cur.read_pointer.null?
entry = cur.read_pointer.read_string
key, value = entry.split '=', 2
value.taint if value
key.taint if key
set_encoding key
set_encoding value
yield key, value
offset += ptr_size
cur = env + offset
end
self
end
alias_method :each_pair, :each
def delete(key)
existing_value = self[key]
self[key] = nil if existing_value
existing_value
end
def delete_if(&block)
return to_enum(:delete_if) unless block_given?
reject!(&block)
self
end
def fetch(key, absent=undefined)
if block_given? and !absent.equal?(undefined)
warn "block supersedes default value argument"
end
if value = self[key]
return value
end
if block_given?
return yield(key)
elsif absent.equal?(undefined)
raise IndexError, "key not found"
end
return absent
end
def include?(key)
!self[key].nil?
end
alias_method :has_key?, :include?
alias_method :key?, :include?
# More efficient than using the one from Enumerable
alias_method :member?, :include?
def to_s
"ENV"
end
def inspect
to_hash.inspect
end
def reject(&block)
to_hash.reject(&block)
end
def reject!
return to_enum(:reject!) unless block_given?
# Avoid deleting from environ while iterating because the
# OS can handle that in a million different bad ways.
keys = []
each { |k, v| keys << k if yield(k, v) }
keys.each { |k| delete k }
keys.empty? ? nil : self
end
def clear
# Avoid deleting from environ while iterating because the
# OS can handle that in a million different bad ways.
keys = []
each { |k, v| keys << k }
keys.each { |k| delete k }
self
end
def has_value?(value)
each { |k, v| return true if v == value }
return false
end
alias_method :value?, :has_value?
def values_at(*params)
params.map{ |k| self[k] }
end
def index(value)
each do |k, v|
return k if v == value
end
nil
end
def invert
to_hash.invert
end
def key(value)
index(value)
end
def keys
keys = []
each { |k, v| keys << k }
keys
end
def values
vals = []
each { |k, v| vals << v }
vals
end
def empty?
each { return false }
return true
end
def length
sz = 0
each { |k, v| sz += 1 }
sz
end
alias_method :size, :length
def rehash
# No need to do anything, our keys are always strings
end
def replace(other)
clear
other.each { |k, v| self[k] = v }
end
def select(&blk)
return to_enum unless block_given?
to_hash.select(&blk)
end
def shift
env = environ()
offset = 0
cur = env + offset
ptr = cur.read_pointer
return nil unless ptr
key, value = ptr.read_string.split "=", 2
return nil unless key
key.taint if key
value.taint if value
delete key
set_encoding key
set_encoding value
return [key, value]
end
def to_a
ary = []
each { |k, v| ary << [k, v] }
ary
end
def to_hash
hsh = {}
each { |k, v| hsh[k] = v }
hsh
end
def update(other, &block)
if block_given?
other.each { |k, v| self[k] = yield(k, self[k], v) }
else
other.each { |k, v| self[k] = v }
end
end
# Missing and deprecated: indexes, indices
end
end