getpwuid(mingw): provide a better default for the user name
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Oct 15, 2018 at 02:47 UTC
564be791f33a72d28497420dafbd3b272b9a1380
1 file changed
+31
-1
compat/mingw.c
+31
-1
@@ -5,6 +5,7 @@
5
#include "../strbuf.h"
6
#include "../run-command.h"
7
#include "../cache.h"
8
+#include "win32/lazyload.h"
9
10
#define HCAST(type, handle) ((type)(intptr_t)handle)
11
@@ -1768,6 +1769,33 @@ int mingw_getpagesize(void)
1769
return si.dwAllocationGranularity;
1770
}
1771
1772
+/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1773
+enum EXTENDED_NAME_FORMAT {
1774
+ NameDisplay = 3,
1775
+ NameUserPrincipal = 8
1776
+};
1777
+
1778
+static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1779
+{
1780
+ DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1781
+ enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1782
+ static wchar_t wbuffer[1024];
1783
+ DWORD len;
1784
+
1785
+ if (!INIT_PROC_ADDR(GetUserNameExW))
1786
+ return NULL;
1787
+
1788
+ len = ARRAY_SIZE(wbuffer);
1789
+ if (GetUserNameExW(type, wbuffer, &len)) {
1790
+ char *converted = xmalloc((len *= 3));
1791
+ if (xwcstoutf(converted, wbuffer, len) >= 0)
1792
+ return converted;
1793
+ free(converted);
1794
+ }
1795
+
1796
+ return NULL;
1797
+}
1798
+
1799
struct passwd *getpwuid(int uid)
1800
{
1801
static unsigned initialized;
@@ -1786,7 +1814,9 @@ struct passwd *getpwuid(int uid)
1814
1815
p = xmalloc(sizeof(*p));
1816
p->pw_name = user_name;
1789
- p->pw_gecos = "unknown";
1817
+ p->pw_gecos = get_extended_user_info(NameDisplay);
1818
+ if (!p->pw_gecos)
1819
+ p->pw_gecos = "unknown";
1820
p->pw_dir = NULL;
1821
1822
initialized = 1;