Changes since initial version (see attached diff for details):
- split in two patches
- removed unused variables
- improved the dll error message
- changed ?: to if else
- added comments

Also available here:
https://github.com/kblees/git/tree/kb/improve-wincred-compatibility-v2
git pull git://github.com/kblees/git.git kb/improve-wincred-compatibility-v2

Karsten Blees (2):
  wincred: accept CRLF on stdin to simplify console usage
  wincred: improve compatibility with windows versions

 .../credential/wincred/git-credential-wincred.c    | 206 ++++++++-------------
 1 file changed, 75 insertions(+), 131 deletions(-)


> git diff kb/improve-wincred-compatibility..kb/improve-wincred-compatibility-v2
diff --git a/contrib/credential/wincred/git-credential-wincred.c 
b/contrib/credential/wincred/git-credential-wincred.c
index 3464080..dac19ea 100644
--- a/contrib/credential/wincred/git-credential-wincred.c
+++ b/contrib/credential/wincred/git-credential-wincred.c
@@ -66,7 +66,7 @@ typedef BOOL (WINAPI *CredEnumerateWT)(LPCWSTR, DWORD, DWORD 
*,
 typedef VOID (WINAPI *CredFreeT)(PVOID);
 typedef BOOL (WINAPI *CredDeleteWT)(LPCWSTR, DWORD, DWORD);
 
-static HMODULE advapi, credui;
+static HMODULE advapi;
 static CredWriteWT CredWriteW;
 static CredEnumerateWT CredEnumerateW;
 static CredFreeT CredFree;
@@ -77,7 +77,7 @@ static void load_cred_funcs(void)
        /* load DLLs */
        advapi = LoadLibrary("advapi32.dll");
        if (!advapi)
-               die("failed to load DLLs");
+               die("failed to load advapi32.dll");
 
        /* get function pointers */
        CredWriteW = (CredWriteWT)GetProcAddress(advapi, "CredWriteW");
@@ -107,14 +107,34 @@ static void write_item(const char *what, LPCWSTR wbuf, 
int wlen)
        free(buf);
 }
 
+/*
+ * Match an (optional) expected string and a delimiter in the target string,
+ * consuming the matched text by updating the target pointer.
+ */
 static int match_part(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
 {
-       LPCWSTR start = *ptarget;
-       LPCWSTR end = *delim ? wcsstr(start, delim) : start + wcslen(start);
-       int len = end ? end - start : wcslen(start);
+       LPCWSTR delim_pos, start = *ptarget;
+       int len;
+
+       /* find start of delimiter (or end-of-string if delim is empty) */
+       if (*delim)
+               delim_pos = wcsstr(start, delim);
+       else
+               delim_pos = start + wcslen(start);
+
+       /*
+        * match text up to delimiter, or end of string (e.g. the '/' after
+        * host is optional if not followed by a path)
+        */
+       if (delim_pos)
+               len = delim_pos - start;
+       else
+               len = wcslen(start);
+
        /* update ptarget if we either found a delimiter or need a match */
-       if (end || want)
-               *ptarget = end ? end + wcslen(delim) : start + len;
+       if (delim_pos || want)
+               *ptarget = delim_pos ? delim_pos + wcslen(delim) : start + len;
+
        return !want || (!wcsncmp(want, start, len) && !want[len]);
 }
 
@@ -157,9 +177,6 @@ static void get_credential(void)
 static void store_credential(void)
 {
        CREDENTIALW cred;
-       BYTE *auth_buf;
-       DWORD auth_buf_size = 0;
-       CREDENTIAL_ATTRIBUTEW attrs[CRED_MAX_ATTRIBUTES];
 
        if (!wusername || !password)
                return;

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to