Skip to content

Commit

Permalink
* win32/win32.c, include/ruby/win32.h (rb_w32_ugetenv): new API to
Browse files Browse the repository at this point in the history
  accept and to return UTF-8 strings.

* win32/win32.c (rb_w32_getenv): follow above change.

* win32/win32.c (rb_w32_get_environ): returns UTF-8 environment area.

* hash.c (env_str_new, rb_f_getenv, env_fetch): follow above changes.
  [Bug ruby#5570] [ruby-core:40737]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
unak committed Mar 15, 2012
1 parent c6671f1 commit fcaee4b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
12 changes: 12 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
Thu Mar 15 12:44:50 2012 NAKAMURA Usaku <[email protected]>

* win32/win32.c, include/ruby/win32.h (rb_w32_ugetenv): new API to
accept and to return UTF-8 strings.

* win32/win32.c (rb_w32_getenv): follow above change.

* win32/win32.c (rb_w32_get_environ): returns UTF-8 environment area.

* hash.c (env_str_new, rb_f_getenv, env_fetch): follow above changes.
[Bug #5570] [ruby-core:40737]

Thu Mar 15 10:57:27 2012 Shugo Maeda <[email protected]>

* enumerator.c (lazy_cycle): add Enumerable::Lazy#cycle.
Expand Down
14 changes: 14 additions & 0 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,8 @@ static char **origenviron;
static char **my_environ;
#undef environ
#define environ my_environ
#undef getenv
#define getenv(n) rb_w32_ugetenv(n)
#elif defined(__APPLE__)
#undef environ
#define environ (*_NSGetEnviron())
Expand All @@ -2029,7 +2031,11 @@ extern char **environ;
static VALUE
env_str_new(const char *ptr, long len)
{
#ifdef _WIN32
VALUE str = rb_str_encode(rb_enc_str_new(ptr, len, rb_utf8_encoding()), rb_enc_from_encoding(rb_locale_encoding()), 0, Qnil);
#else
VALUE str = rb_locale_str_new(ptr, len);
#endif

rb_obj_freeze(str);
return str;
Expand Down Expand Up @@ -2108,7 +2114,11 @@ rb_f_getenv(VALUE obj, VALUE name)
env = getenv(nam);
if (env) {
if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
#ifdef _WIN32
VALUE str = rb_str_encode(rb_enc_str_new(env, strlen(env), rb_utf8_encoding()), rb_enc_from_encoding(rb_filesystem_encoding()), 0, Qnil);
#else
VALUE str = rb_filesystem_str_new_cstr(env);
#endif

rb_obj_freeze(str);
return str;
Expand Down Expand Up @@ -2159,7 +2169,11 @@ env_fetch(int argc, VALUE *argv)
return if_none;
}
if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env))
#ifdef _WIN32
return rb_str_encode(rb_enc_str_new(env, strlen(env), rb_utf8_encoding()), rb_enc_from_encoding(rb_filesystem_encoding()), 0, Qnil);
#else
return rb_filesystem_str_new_cstr(env);
#endif
return env_str_new2(env);
}

Expand Down
1 change: 1 addition & 0 deletions include/ruby/win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ extern struct servent *WSAAPI rb_w32_getservbyname(const char *, const char *);
extern struct servent *WSAAPI rb_w32_getservbyport(int, const char *);
extern int rb_w32_socketpair(int, int, int, int *);
extern char * rb_w32_getcwd(char *, int);
extern char * rb_w32_ugetenv(const char *);
extern char * rb_w32_getenv(const char *);
extern int rb_w32_rename(const char *, const char *);
extern int rb_w32_urename(const char *, const char *);
Expand Down
67 changes: 59 additions & 8 deletions win32/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ static CRITICAL_SECTION select_mutex;
static int NtSocketsInitialized = 0;
static st_table *socklist = NULL;
static char *envarea;
static char *uenvarea;

/* License: Ruby's */
static void
Expand All @@ -648,6 +649,10 @@ exit_handler(void)
FreeEnvironmentStrings(envarea);
envarea = NULL;
}
if (uenvarea) {
free(uenvarea);
uenvarea = NULL;
}
}

/* License: Artistic or GPL */
Expand Down Expand Up @@ -4234,6 +4239,44 @@ wait(int *status)
return waitpid(-1, status, 0);
}

/* License: Ruby's */
char *
rb_w32_ugetenv(const char *name)
{
WCHAR *wenvarea, *wenv;
int len = strlen(name);
char *env;
int wlen;

if (len == 0) return NULL;

if (uenvarea) {
free(uenvarea);
uenvarea = NULL;
}
if (envarea) {
FreeEnvironmentStrings(envarea);
envarea = NULL;
}
wenvarea = GetEnvironmentStringsW();
if (!wenvarea) {
map_errno(GetLastError());
return NULL;
}
for (wenv = wenvarea, wlen = 1; *wenv; wenv += lstrlenW(wenv) + 1)
wlen += lstrlenW(wenv) + 1;
uenvarea = wstr_to_mbstr(CP_UTF8, wenvarea, wlen, NULL);
FreeEnvironmentStringsW(wenvarea);
if (!uenvarea)
return NULL;

for (env = uenvarea; *env; env += strlen(env) + 1)
if (strncasecmp(env, name, len) == 0 && *(env + len) == '=')
return env + len + 1;

return NULL;
}

/* License: Ruby's */
char *
rb_w32_getenv(const char *name)
Expand All @@ -4242,7 +4285,14 @@ rb_w32_getenv(const char *name)
char *env;

if (len == 0) return NULL;
if (envarea) FreeEnvironmentStrings(envarea);
if (uenvarea) {
free(uenvarea);
uenvarea = NULL;
}
if (envarea) {
FreeEnvironmentStrings(envarea);
envarea = NULL;
}
envarea = GetEnvironmentStrings();
if (!envarea) {
map_errno(GetLastError());
Expand Down Expand Up @@ -5011,7 +5061,7 @@ rb_w32_asynchronize(asynchronous_func_t func, uintptr_t self,
char **
rb_w32_get_environ(void)
{
char *envtop, *env;
WCHAR *envtop, *env;
char **myenvtop, **myenv;
int num;

Expand All @@ -5022,23 +5072,24 @@ rb_w32_get_environ(void)
* CygWin deals these values by changing first `=' to '!'. But we don't
* use such trick and follow cmd.exe's way that just doesn't show these
* values.
* (U.N. 2001-11-15)
*
* This function returns UTF-8 strings.
*/
envtop = GetEnvironmentStrings();
for (env = envtop, num = 0; *env; env += strlen(env) + 1)
envtop = GetEnvironmentStringsW();
for (env = envtop, num = 0; *env; env += lstrlenW(env) + 1)
if (*env != '=') num++;

myenvtop = (char **)malloc(sizeof(char *) * (num + 1));
for (env = envtop, myenv = myenvtop; *env; env += strlen(env) + 1) {
for (env = envtop, myenv = myenvtop; *env; env += lstrlenW(env) + 1) {
if (*env != '=') {
if (!(*myenv = strdup(env))) {
if (!(*myenv = wstr_to_utf8(env, NULL))) {
break;
}
myenv++;
}
}
*myenv = NULL;
FreeEnvironmentStrings(envtop);
FreeEnvironmentStringsW(envtop);

return myenvtop;
}
Expand Down

0 comments on commit fcaee4b

Please sign in to comment.