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

[PATCH 2/3] getpwuid(mingw): provide a better default for the user name

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 15, 2018, 09:47 UTC
Message-ID
<11025b1639785577924b2020fb20d076308e9c69.1539596822.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.45.git.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

We do have the excellent GetUserInfoEx() function to obtain more detailed information of the current user (if the user is part of a Windows domain); Let's use it.

Suggested by Lutz Roeder.

To avoid the cost of loading Secur32.dll (even lazily, loading DLLs takes a non-neglibile amount of time), we use the established technique to load DLLs only when, and if, needed.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/mingw.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index 597781b370..623ff5daf5 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -5,6 +5,7 @@
 #include "../strbuf.h"
 #include "../run-command.h"
 #include "../cache.h"
+#include "win32/lazyload.h"
 
 #define HCAST(type, handle) ((type)(intptr_t)handle)
 
@@ -1798,6 +1799,33 @@ int mingw_getpagesize(void)
 	return si.dwAllocationGranularity;
 }
 
+/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
+enum EXTENDED_NAME_FORMAT {
+	NameDisplay = 3,
+	NameUserPrincipal = 8
+};
+
+static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
+{
+	DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
+		enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
+	static wchar_t wbuffer[1024];
+	DWORD len;
+
+	if (!INIT_PROC_ADDR(GetUserNameExW))
+		return NULL;
+
+	len = ARRAY_SIZE(wbuffer);
+	if (GetUserNameExW(type, wbuffer, &len)) {
+		char *converted = xmalloc((len *= 3));
+		if (xwcstoutf(converted, wbuffer, len) >= 0)
+			return converted;
+		free(converted);
+	}
+
+	return NULL;
+}
+
 struct passwd *getpwuid(int uid)
 {
 	static unsigned initialized;
@@ -1816,7 +1844,9 @@ struct passwd *getpwuid(int uid)
 
 	p = xmalloc(sizeof(*p));
 	p->pw_name = user_name;
-	p->pw_gecos = "unknown";
+	p->pw_gecos = get_extended_user_info(NameDisplay);
+	if (!p->pw_gecos)
+		p->pw_gecos = "unknown";
 	p->pw_dir = NULL;
 
 	initialized = 1;
-- 
gitgitgadget
Previous: Eric SunshineNext: Eric Sunshine
Message 4 of 12 in “Provide a useful default user ident on Windows”
  1. 0/3 Provide a useful default user ident on WindowsJohannes Schindelin via GitGitGadget, Oct 15, 2018
  2. 1/3 getpwuid(mingw): initialize the structure only onceJohannes Schindelin via GitGitGadget, Oct 15, 2018
  3. Eric SunshineOct 15, 2018
  4. 2/3 getpwuid(mingw): provide a better default for the user nameJohannes Schindelin via GitGitGadget, Oct 15, 2018
  5. Eric SunshineOct 15, 2018
  6. Johannes SchindelinOct 16, 2018
  7. Eric SunshineOct 16, 2018
  8. Johannes SchindelinOct 16, 2018
  9. 3/3 mingw: use domain information for default emailJohannes Schindelin via GitGitGadget, Oct 15, 2018
  10. Eric SunshineOct 15, 2018
  11. Johannes SchindelinOct 16, 2018
  12. Junio C HamanoOct 16, 2018

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.