From 8c92516f2dae4778e2b5a41196f7409823283e4b Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 6 Jun 2011 08:59:10 +0200 Subject: sh-i18n--envsubst: do not crash when no arguments are given Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano diff --git a/sh-i18n--envsubst.c b/sh-i18n--envsubst.c index 7125093..8104973 100644 --- a/sh-i18n--envsubst.c +++ b/sh-i18n--envsubst.c @@ -74,6 +74,7 @@ main (int argc, char *argv[]) { case 1: error ("we won't substitute all variables on stdin for you"); + break; /* all_variables = 1; subst_from_stdin (); -- cgit v0.10.2-6-g49f6 From 06bc4b796ad69ba93f0a8c451368602e0553c2d3 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 6 Jun 2011 09:06:02 +0200 Subject: mingw.c: move definition of mingw_getenv down We want to use static lookup_env() in a subsequent change. At first sight, this change looks innocent. But it is not due to the #undef getenv. There is one caller of getenv between the old location and the new location whose behavior could change. But as can be seen from the defintion of mingw_getenv, the behavior for this caller does not change substantially. To ensure consistent behavior in the future, change all getenv callers in mingw.c to use mingw_getenv. With this patch, this is not a big deal, yet, but with the subsequent change, where we teach getenv to do a case-sensitive lookup, the behavior of all call sites is changed. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano diff --git a/compat/mingw.c b/compat/mingw.c index 4423961..e341ac2 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -178,7 +178,7 @@ static int ask_yes_no_if_possible(const char *format, ...) vsnprintf(question, sizeof(question), format, args); va_end(args); - if ((retry_hook[0] = getenv("GIT_ASK_YESNO"))) { + if ((retry_hook[0] = mingw_getenv("GIT_ASK_YESNO"))) { retry_hook[1] = question; return !run_command_v_opt(retry_hook, 0); } @@ -599,19 +599,6 @@ char *mingw_getcwd(char *pointer, int len) return ret; } -#undef getenv -char *mingw_getenv(const char *name) -{ - char *result = getenv(name); - if (!result && !strcmp(name, "TMPDIR")) { - /* on Windows it is TMP and TEMP */ - result = getenv("TMP"); - if (!result) - result = getenv("TEMP"); - } - return result; -} - /* * See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx * (Parsing C++ Command-Line Arguments) @@ -711,7 +698,7 @@ static const char *parse_interpreter(const char *cmd) */ static char **get_path_split(void) { - char *p, **path, *envpath = getenv("PATH"); + char *p, **path, *envpath = mingw_getenv("PATH"); int i, n = 0; if (!envpath || !*envpath) @@ -1128,6 +1115,19 @@ char **make_augmented_environ(const char *const *vars) return env; } +#undef getenv +char *mingw_getenv(const char *name) +{ + char *result = getenv(name); + if (!result && !strcmp(name, "TMPDIR")) { + /* on Windows it is TMP and TEMP */ + result = getenv("TMP"); + if (!result) + result = getenv("TEMP"); + } + return result; +} + /* * Note, this isn't a complete replacement for getaddrinfo. It assumes * that service contains a numerical port, or that it is null. It -- cgit v0.10.2-6-g49f6 From df599e9612788b728ce43a03159b85f1fe624d6a Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 6 Jun 2011 09:08:13 +0200 Subject: Windows: teach getenv to do a case-sensitive search 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 Signed-off-by: Junio C Hamano diff --git a/compat/mingw.c b/compat/mingw.c index e341ac2..237a8c0 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; } -- cgit v0.10.2-6-g49f6