264
static char **utf8_environ;
265
static void *utf8_environ_ctx;
267
static void free_env(void)
269
talloc_free(utf8_environ_ctx);
270
utf8_environ_ctx = NULL;
274
// Note: UNIX getenv() returns static strings, and we try to do the same. Since
275
// using putenv() is not multithreading safe, we don't expect env vars to change
276
// at runtime, and converting/allocating them in advance is ok.
277
static void init_getenv(void)
279
if (utf8_environ_ctx)
281
wchar_t *wenv = GetEnvironmentStringsW();
284
utf8_environ_ctx = talloc_new(NULL);
287
size_t len = wcslen(wenv);
290
char *s = mp_to_utf8(utf8_environ_ctx, wenv);
291
MP_TARRAY_APPEND(utf8_environ_ctx, utf8_environ, num_env, s);
294
MP_TARRAY_APPEND(utf8_environ_ctx, utf8_environ, num_env, NULL);
295
// Avoid showing up in leak detectors etc.
299
char *mp_getenv(const char *name)
302
static pthread_once_t once_init_getenv = PTHREAD_ONCE_INIT;
303
pthread_once(&once_init_getenv, init_getenv);
307
// Copied from musl, http://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
308
// Copyright © 2005-2013 Rich Felker, standard MIT license
310
size_t l = strlen(name);
311
if (!utf8_environ || !*name || strchr(name, '=')) return NULL;
312
for (i=0; utf8_environ[i] && (strncmp(name, utf8_environ[i], l)
313
|| utf8_environ[i][l] != '='); i++) {}
314
if (utf8_environ[i]) return utf8_environ[i] + l+1;
258
318
#endif // __MINGW32__