mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
hash.c: utility functions from ruby_setenv
* hash.c (invalid_envname, check_envname): extract utility functions from ruby_setenv(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42933 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
24833587d3
commit
85995e88d4
1 changed files with 28 additions and 12 deletions
40
hash.c
40
hash.c
|
@ -2585,16 +2585,32 @@ getenvblocksize()
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
|
||||||
|
NORETURN(static void invalid_envname(const char *name));
|
||||||
|
|
||||||
|
static void
|
||||||
|
invalid_envname(const char *name)
|
||||||
|
{
|
||||||
|
rb_syserr_fail_str(EINVAL, rb_sprintf("ruby_setenv(%s)", name));
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
check_envname(const char *name)
|
||||||
|
{
|
||||||
|
if (strchr(name, '=')) {
|
||||||
|
invalid_envname(name);
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void
|
void
|
||||||
ruby_setenv(const char *name, const char *value)
|
ruby_setenv(const char *name, const char *value)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
VALUE buf;
|
VALUE buf;
|
||||||
int failed = 0;
|
int failed = 0;
|
||||||
if (strchr(name, '=')) {
|
check_envname(name);
|
||||||
fail:
|
|
||||||
rb_syserr_fail_str(EINVAL, rb_sprintf("ruby_setenv(%s)", name));
|
|
||||||
}
|
|
||||||
if (value) {
|
if (value) {
|
||||||
const char* p = GetEnvironmentStringsA();
|
const char* p = GetEnvironmentStringsA();
|
||||||
if (!p) goto fail; /* never happen */
|
if (!p) goto fail; /* never happen */
|
||||||
|
@ -2615,14 +2631,18 @@ ruby_setenv(const char *name, const char *value)
|
||||||
if (!SetEnvironmentVariable(name, value) &&
|
if (!SetEnvironmentVariable(name, value) &&
|
||||||
GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
|
GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
|
||||||
}
|
}
|
||||||
if (failed) goto fail;
|
if (failed) {
|
||||||
|
fail:
|
||||||
|
invalid_envname(name);
|
||||||
|
}
|
||||||
#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
|
#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
|
||||||
#undef setenv
|
#undef setenv
|
||||||
#undef unsetenv
|
#undef unsetenv
|
||||||
if (value) {
|
if (value) {
|
||||||
if (setenv(name, value, 1))
|
if (setenv(name, value, 1))
|
||||||
rb_sys_fail_str(rb_sprintf("setenv(%s)", name));
|
rb_sys_fail_str(rb_sprintf("setenv(%s)", name));
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
#ifdef VOID_UNSETENV
|
#ifdef VOID_UNSETENV
|
||||||
unsetenv(name);
|
unsetenv(name);
|
||||||
#else
|
#else
|
||||||
|
@ -2633,9 +2653,7 @@ ruby_setenv(const char *name, const char *value)
|
||||||
#elif defined __sun
|
#elif defined __sun
|
||||||
size_t len;
|
size_t len;
|
||||||
char **env_ptr, *str;
|
char **env_ptr, *str;
|
||||||
if (strchr(name, '=')) {
|
|
||||||
rb_syserr_fail_str(EINVAL, rb_sprintf("ruby_setenv(%s)", name));
|
|
||||||
}
|
|
||||||
len = strlen(name);
|
len = strlen(name);
|
||||||
for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
|
for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
|
||||||
if (!strncmp(str, name, len) && str[len] == '=') {
|
if (!strncmp(str, name, len) && str[len] == '=') {
|
||||||
|
@ -2653,9 +2671,7 @@ ruby_setenv(const char *name, const char *value)
|
||||||
#else /* WIN32 */
|
#else /* WIN32 */
|
||||||
size_t len;
|
size_t len;
|
||||||
int i;
|
int i;
|
||||||
if (strchr(name, '=')) {
|
|
||||||
rb_syserr_fail_str(EINVAL, rb_sprintf("ruby_setenv(%s)", name));
|
|
||||||
}
|
|
||||||
i=envix(name); /* where does it go? */
|
i=envix(name); /* where does it go? */
|
||||||
|
|
||||||
if (environ == origenviron) { /* need we copy environment? */
|
if (environ == origenviron) { /* need we copy environment? */
|
||||||
|
|
Loading…
Add table
Reference in a new issue