Bug #3812 ยป setenv.patch
| hash.c | ||
|---|---|---|
|
#if defined(_WIN32)
|
||
|
int len;
|
||
|
char *buf;
|
||
|
if (strchr(name, '=')) {
|
||
|
errno = EINVAL;
|
||
|
rb_sys_fail("ruby_setenv");
|
||
|
}
|
||
|
if (value) {
|
||
|
len = strlen(name) + 1 + strlen(value) + 1;
|
||
|
buf = ALLOCA_N(char, len);
|
||
|
snprintf(buf, len, "%s=%s", name, value);
|
||
|
putenv(buf);
|
||
|
/* putenv() doesn't handle empty value */
|
||
|
if (!*value)
|
||
|
SetEnvironmentVariable(name,value);
|
||
|
}
|
||
|
else {
|
||
|
len = strlen(name) + 1 + 1;
|
||
|
buf = ALLOCA_N(char, len);
|
||
|
snprintf(buf, len, "%s=", name);
|
||
|
putenv(buf);
|
||
|
SetEnvironmentVariable(name, 0);
|
||
|
}
|
||
|
const char *new_value = value ? value : "";
|
||
|
if (strchr(name, '=')) goto fail;
|
||
|
len = strlen(name) + 1 + strlen(new_value) + 1;
|
||
|
buf = ALLOCA_N(char, len);
|
||
|
if (!buf) goto fail;
|
||
|
snprintf(buf, len, "%s=%s", name, new_value);
|
||
|
if (putenv(buf)) goto fail;
|
||
|
if ((!value || !*value) && !SetEnvironmentVariable(name,value))
|
||
|
goto fail;
|
||
|
return;
|
||
|
fail:
|
||
|
errno = EINVAL;
|
||
|
rb_sys_fail("ruby_setenv");
|
||
|
#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
|
||
|
#undef setenv
|
||
|
#undef unsetenv
|
||
| test/ruby/test_env.rb | ||
|---|---|---|
|
require 'test/unit'
|
||
|
require 'rbconfig'
|
||
|
class TestEnv < Test::Unit::TestCase
|
||
|
IGNORE_CASE = /bccwin|mswin|mingw/ =~ RUBY_PLATFORM
|
||
| ... | ... | |
|
ENV.update({"baz"=>"quux","a"=>"b"}) {|k, v1, v2| v1 ? k + "_" + v1 + "_" + v2 : v2 }
|
||
|
check(ENV.to_hash.to_a, [%w(foo bar), %w(baz baz_qux_quux), %w(a b)])
|
||
|
end
|
||
|
def test_huge_value
|
||
|
huge_value = "bar" * 40960
|
||
|
ENV["foo"] = "bar"
|
||
|
if Config::CONFIG['host_os'] =~ /mswin|mingw/
|
||
|
assert_raise(Errno::EINVAL) { ENV["foo"] = huge_value }
|
||
|
assert_equal("bar", ENV["foo"])
|
||
|
else
|
||
|
assert_nothing_raised { ENV["foo"] = huge_value }
|
||
|
assert_equal(huge_value, ENV["foo"])
|
||
|
end
|
||
|
end
|
||
|
end
|
||