- if (OPENSSL_issetugid() == 0)
- s = getenv("RANDFILE");
- if (s != NULL && *s && strlen(s) + 1 < size) {
- if (OPENSSL_strlcpy(buf, s, size) >= size)
- return NULL;
- } else {
-#ifdef OPENSSL_SYS_WINDOWS
- /*
- * We use the same env variables as GetTempFile() - but that function
- * uses TCHARs, but getenv() gives us chars so its easier to do it this
- * way
- */
- if ((s = getenv("TMP")) == NULL
- && (s = getenv("TEMP")) == NULL
- && (s = getenv("USERPROFILE")) == NULL
- && (s = getenv("SYSTEMROOT")) == NULL) {
- s = getenv("HOME");
+#if defined(_WIN32) && defined(CP_UTF8)
+ DWORD envlen;
+ WCHAR *var;
+
+ /* Look up various environment variables. */
+ if ((envlen = GetEnvironmentVariableW(var = L"RANDFILE", NULL, 0)) == 0) {
+ use_randfile = 0;
+ if ((envlen = GetEnvironmentVariableW(var = L"HOME", NULL, 0)) == 0
+ && (envlen = GetEnvironmentVariableW(var = L"USERPROFILE",
+ NULL, 0)) == 0)
+ envlen = GetEnvironmentVariableW(var = L"SYSTEMROOT", NULL, 0);
+ }
+
+ /* If we got a value, allocate space to hold it and then get it. */
+ if (envlen != 0) {
+ int sz;
+ WCHAR *val = _alloca(envlen * sizeof(WCHAR));
+
+ if (GetEnvironmentVariableW(var, val, envlen) < envlen
+ && (sz = WideCharToMultiByte(CP_UTF8, 0, val, -1, NULL, 0,
+ NULL, NULL)) != 0) {
+ s = _alloca(sz);
+ if (WideCharToMultiByte(CP_UTF8, 0, val, -1, s, sz,
+ NULL, NULL) == 0)
+ s = NULL;