From a79479d5bb9bf61e395e954da10ca2b4369f1972 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 4 May 2015 06:34:52 -0700 Subject: [PATCH] git-wrapper: prepare to allow more options than MINIMAL_PATH With the resource-driven command-line configuration, we introduced the option to ensure that only the PATH environment variable is edited only minimally, i.e. only /cmd/ is added (as appropriate for _Git CMD_). We are about to add another option, so let's refactor the equivalent of Git's `strip_prefix()` function; It is not *quite* the same because we have to `memmove()` the remainder to the beginning of the buffer. Signed-off-by: Johannes Schindelin --- compat/win32/git-wrapper.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/compat/win32/git-wrapper.c b/compat/win32/git-wrapper.c index b5e5ae899e..20e8936eef 100644 --- a/compat/win32/git-wrapper.c +++ b/compat/win32/git-wrapper.c @@ -168,6 +168,22 @@ static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait, return cmd; } +static int strip_prefix(LPWSTR str, int *len, LPCWSTR prefix) +{ + LPWSTR start = str; + do { + if (str - start > *len) + return 0; + if (!*prefix) { + *len -= str - start; + memmove(start, str, + sizeof(WCHAR) * (wcslen(str) + 1)); + return 1; + } + } while (*str++ == *prefix++); + return 0; +} + static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep, LPWSTR *prefix_args, int *prefix_args_len, int *is_git_command, LPWSTR *working_directory, int *full_path, @@ -199,10 +215,11 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep, exit(1); } - if (!wcsncmp(L"MINIMAL_PATH=1 ", buf, 15)) { - minimal_search_path = 1; - memmove(buf, buf + 15, - sizeof(WCHAR) * (wcslen(buf + 15) + 1)); + for (;;) { + if (strip_prefix(buf, &len, L"MINIMAL_PATH=1 ")) + minimal_search_path = 1; + else + break; } buf[len] = L'\0';