From e5fc63e7b73c942d6f584b5a2f1376cacfce3a62 Mon Sep 17 00:00:00 2001 From: Karsten Blees Date: Mon, 25 Apr 2011 23:32:27 +0100 Subject: [PATCH] Win32: Unicode environment (incoming) Convert environment from UTF-16 to UTF-8 on startup. No changes to getenv() are necessary, as the MSVCRT version is implemented on top of char **environ. However, putenv / _wputenv from MSVCRT no longer work, for two reasons: 1. they try to keep environ, _wenviron and the Win32 process environment in sync, using the default system encoding instead of UTF-8 to convert between charsets 2. msysgit and MSVCRT use different allocators, memory allocated in git cannot be freed by the CRT and vice versa Implement mingw_putenv using the env_setenv helper function from the environment merge code. Note that in case of memory allocation failure, putenv now dies with error message (due to xrealloc) instead of failing with ENOMEM. As git assumes setenv / putenv to always succeed, this prevents it from continuing with incorrect settings. Signed-off-by: Karsten Blees --- compat/mingw.c | 15 +++++++++++++++ compat/mingw.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 7e82b519ae..727e389025 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1284,6 +1284,12 @@ char **make_augmented_environ(const char *const *vars) return env; } +int mingw_putenv(const char *namevalue) +{ + environ = env_setenv(environ, namevalue); + return 0; +} + /* * Note, this isn't a complete replacement for getaddrinfo. It assumes * that service contains a numerical port, or that it is null. It @@ -2066,6 +2072,11 @@ void mingw_startup() maxlen = wcslen(_wpgmptr); for (i = 1; i < argc; i++) maxlen = max(maxlen, wcslen(wargv[i])); + for (i = 0; wenv[i]; i++) + maxlen = max(maxlen, wcslen(wenv[i])); + + /* nedmalloc can't free CRT memory, allocate resizable environment list */ + environ = xcalloc(i + 1, sizeof(char*)); /* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */ maxlen = 3 * maxlen + 1; @@ -2078,6 +2089,10 @@ void mingw_startup() len = xwcstoutf(buffer, wargv[i], maxlen); __argv[i] = xmemdupz(buffer, len); } + for (i = 0; wenv[i]; i++) { + len = xwcstoutf(buffer, wenv[i], maxlen); + environ[i] = xmemdupz(buffer, len); + } free(buffer); /* initialize critical section for waitpid pinfo_t list */ diff --git a/compat/mingw.h b/compat/mingw.h index b06fb8917c..8980101ba1 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -200,6 +200,8 @@ char *mingw_getcwd(char *pointer, int len); char *mingw_getenv(const char *name); #define getenv mingw_getenv +int mingw_putenv(const char *namevalue); +#define putenv mingw_putenv int mingw_gethostname(char *host, int namelen); #define gethostname mingw_gethostname -- 2.11.4.GIT