Windows: teach getenv to do a case-sensitive search
authorJohannes Sixt <j6t@kdbg.org>
Mon, 6 Jun 2011 07:08:13 +0000 (09:08 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 6 Jun 2011 18:35:10 +0000 (11:35 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
compat/mingw.c
index e341ac2abd20f0d802773ca6d37dc44366abda79..237a8c0e1f485fec2f0e6f1f1fdbbd1163fb7c89 100644 (file)
@@ -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;
 }