2023-12-07 01:29:52 +01:00
|
|
|
lib/getenv.c | 47 ++++++++++++++++++++++++++++++-----------------
|
|
|
|
1 file changed, 30 insertions(+), 17 deletions(-)
|
2022-06-28 22:45:53 +02:00
|
|
|
|
|
|
|
diff --git a/lib/getenv.c b/lib/getenv.c
|
2024-08-01 21:29:19 +02:00
|
|
|
index 49a2e50..dbf2642 100644
|
2022-06-28 22:45:53 +02:00
|
|
|
--- a/lib/getenv.c
|
|
|
|
+++ b/lib/getenv.c
|
2023-12-07 01:29:52 +01:00
|
|
|
@@ -26,26 +26,23 @@
|
2021-06-20 16:22:12 +02:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "curl_memory.h"
|
|
|
|
+#include "curlx.h"
|
2021-06-20 14:44:55 +02:00
|
|
|
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
-static char *GetEnv(const char *variable)
|
2023-12-07 01:29:52 +01:00
|
|
|
+#if defined(_WIN32)
|
|
|
|
+static wchar_t *GetEnv(const wchar_t *variable)
|
2021-06-20 14:44:55 +02:00
|
|
|
{
|
2023-12-07 01:29:52 +01:00
|
|
|
-#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP) || \
|
|
|
|
- defined(__ORBIS__) || defined(__PROSPERO__) /* PlayStation 4 and 5 */
|
2021-06-20 14:44:55 +02:00
|
|
|
- (void)variable;
|
|
|
|
- return NULL;
|
2023-12-07 01:29:52 +01:00
|
|
|
-#elif defined(_WIN32)
|
2021-06-20 14:44:55 +02:00
|
|
|
/* This uses Windows API instead of C runtime getenv() to get the environment
|
2024-08-01 21:29:19 +02:00
|
|
|
variable since some changes are not always visible to the latter. #4774 */
|
2021-06-20 14:44:55 +02:00
|
|
|
- char *buf = NULL;
|
|
|
|
- char *tmp;
|
2023-12-07 01:29:52 +01:00
|
|
|
+ wchar_t *buf = NULL;
|
|
|
|
+ wchar_t *tmp;
|
2021-06-20 14:44:55 +02:00
|
|
|
DWORD bufsize;
|
|
|
|
DWORD rc = 1;
|
|
|
|
const DWORD max = 32768; /* max env var size from MSCRT source */
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
- tmp = realloc(buf, rc);
|
2023-12-07 01:29:52 +01:00
|
|
|
+ tmp = (wchar_t*)realloc(buf, rc * sizeof(wchar_t));
|
2021-06-20 14:44:55 +02:00
|
|
|
if(!tmp) {
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
2023-12-07 01:29:52 +01:00
|
|
|
@@ -56,25 +53,41 @@ static char *GetEnv(const char *variable)
|
2021-06-20 14:44:55 +02:00
|
|
|
|
2024-08-01 21:29:19 +02:00
|
|
|
/* it is possible for rc to be 0 if the variable was found but empty.
|
|
|
|
Since getenv does not make that distinction we ignore it as well. */
|
2021-06-20 14:44:55 +02:00
|
|
|
- rc = GetEnvironmentVariableA(variable, buf, bufsize);
|
2023-12-07 01:29:52 +01:00
|
|
|
+ rc = GetEnvironmentVariableW(variable, buf, bufsize);
|
2021-06-20 14:44:55 +02:00
|
|
|
if(!rc || rc == bufsize || rc > max) {
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if rc < bufsize then rc is bytes written not including null */
|
|
|
|
- if(rc < bufsize)
|
|
|
|
+ if(rc < bufsize) {
|
|
|
|
return buf;
|
|
|
|
-
|
|
|
|
+ }
|
|
|
|
/* else rc is bytes needed, try again */
|
|
|
|
}
|
|
|
|
+}
|
|
|
|
+#endif
|
|
|
|
+
|
2021-06-20 16:22:12 +02:00
|
|
|
+char *curl_getenv(const char *variable)
|
2021-06-20 14:44:55 +02:00
|
|
|
+{
|
2023-12-07 01:29:52 +01:00
|
|
|
+#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP) || \
|
|
|
|
+ defined(__ORBIS__) || defined(__PROSPERO__) /* PlayStation 4 and 5 */
|
2021-06-20 14:44:55 +02:00
|
|
|
+ (void)variable;
|
|
|
|
+ return NULL;
|
2023-12-07 01:29:52 +01:00
|
|
|
+#elif defined(_WIN32)
|
2021-06-20 14:44:55 +02:00
|
|
|
+ char *value = NULL;
|
|
|
|
+ wchar_t *variable_w = curlx_convert_UTF8_to_wchar(variable);
|
|
|
|
+ if(variable_w) {
|
2021-06-20 16:22:12 +02:00
|
|
|
+ wchar_t *value_w = GetEnv(variable_w);
|
2021-06-20 14:44:55 +02:00
|
|
|
+ if(value_w) {
|
|
|
|
+ value = curlx_convert_wchar_to_UTF8(value_w);
|
|
|
|
+ free(value_w);
|
|
|
|
+ }
|
|
|
|
+ free(variable_w);
|
|
|
|
+ }
|
|
|
|
+ return value;
|
|
|
|
#else
|
|
|
|
char *env = getenv(variable);
|
|
|
|
return (env && env[0])?strdup(env):NULL;
|
2021-06-20 16:22:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
-
|
|
|
|
-char *curl_getenv(const char *v)
|
|
|
|
-{
|
|
|
|
- return GetEnv(v);
|
|
|
|
-}
|