Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 28ec93e

Browse files
kbleeskasal
authored andcommitted
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 <[email protected]>
1 parent 410b91b commit 28ec93e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

compat/mingw.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,12 @@ char **make_augmented_environ(const char *const *vars)
12661266
return env;
12671267
}
12681268

1269+
int mingw_putenv(const char *namevalue)
1270+
{
1271+
environ = env_setenv(environ, namevalue);
1272+
return 0;
1273+
}
1274+
12691275
/*
12701276
* Note, this isn't a complete replacement for getaddrinfo. It assumes
12711277
* that service contains a numerical port, or that it is null. It
@@ -2027,6 +2033,11 @@ void mingw_startup()
20272033
maxlen = wcslen(_wpgmptr);
20282034
for (i = 1; i < argc; i++)
20292035
maxlen = max(maxlen, wcslen(wargv[i]));
2036+
for (i = 0; wenv[i]; i++)
2037+
maxlen = max(maxlen, wcslen(wenv[i]));
2038+
2039+
/* nedmalloc can't free CRT memory, allocate resizable environment list */
2040+
environ = xcalloc(i + 1, sizeof(char*));
20302041

20312042
/* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */
20322043
maxlen = 3 * maxlen + 1;
@@ -2039,6 +2050,10 @@ void mingw_startup()
20392050
len = xwcstoutf(buffer, wargv[i], maxlen);
20402051
__argv[i] = xmemdupz(buffer, len);
20412052
}
2053+
for (i = 0; wenv[i]; i++) {
2054+
len = xwcstoutf(buffer, wenv[i], maxlen);
2055+
environ[i] = xmemdupz(buffer, len);
2056+
}
20422057
free(buffer);
20432058

20442059
/* initialize critical section for waitpid pinfo_t list */

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ char *mingw_getcwd(char *pointer, int len);
207207

208208
char *mingw_getenv(const char *name);
209209
#define getenv mingw_getenv
210+
int mingw_putenv(const char *namevalue);
211+
#define putenv mingw_putenv
210212

211213
int mingw_gethostname(char *host, int namelen);
212214
#define gethostname mingw_gethostname

0 commit comments

Comments
 (0)