git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] Windows: teach getenv to do a case-sensitive search

From
Johannes Sixt <j.sixt@viscovery.net>
Date
Jun 6, 2011, 07:08 UTC
Message-ID
<4DEC7CDD.10403@viscovery.net>
In-Reply-To
<4DEC7A65.7020207@viscovery.net>
From: Johannes Sixt <j6t@kdbg.org>

getenv() on Windows looks up environment variables in a case-insensitive manner. Even though all documentations claim that the environment is case-insensitive, it is possible for applications to pass an environment to child processes that has variables that differ only in case. Bash on Windows does this, for example, and sh-i18n--envsubst depends on this behavior.

With this patch environment variables are first looked up in a case-sensitive manner; only if this finds nothing, the system's getenv() is used as a fallback.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 compat/mingw.c |   23 ++++++++++++++++++++---
 1 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index ee480f9..6e5af32 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1116,14 +1116,31 @@ char **make_augmented_environ(const char *const *vars)
 }
 
 #undef getenv
+
+/*
+ * The system's getenv looks up the name in a case-insensitive manner.
+ * This version tries a case-sensitive lookup and falls back to
+ * case-insensitive if nothing was found.  This is necessary because,
+ * as a prominent example, CMD sets 'Path', but not 'PATH'.
+ * Warning: not thread-safe.
+ */
+static char *getenv_cs(const char *name)
+{
+	size_t len = strlen(name);
+	int i = lookup_env(environ, name, len);
+	if (i >= 0)
+		return environ[i] + len + 1;	/* skip past name and '=' */
+	return getenv(name);
+}
+
 char *mingw_getenv(const char *name)
 {
-	char *result = getenv(name);
+	char *result = getenv_cs(name);
 	if (!result && !strcmp(name, "TMPDIR")) {
 		/* on Windows it is TMP and TEMP */
-		result = getenv("TMP");
+		result = getenv_cs("TMP");
 		if (!result)
-			result = getenv("TEMP");
+			result = getenv_cs("TEMP");
 	}
 	return result;
 }
-- 
1.7.6.rc0.1186.gfb4fd
Previous: Johannes SixtNext: Karsten Blees
Message 4 of 5 in “sh-i18n--envsubst; case-sensitive getenv on Windows”
  1. 0/3 sh-i18n--envsubst; case-sensitive getenv on WindowsJohannes Sixt, Jun 6, 2011
  2. 1/3 sh-i18n--envsubst: do not crash when no arguments are givenJohannes Sixt, Jun 6, 2011
  3. 2/3 mingw.c: move definition of mingw_getenv downJohannes Sixt, Jun 6, 2011
  4. 3/3 Windows: teach getenv to do a case-sensitive searchJohannes Sixt, Jun 6, 2011
  5. Karsten BleesOct 5, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.