Skip to content

Commit dec010d

Browse files
committed
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 <[email protected]>
1 parent 616d1c8 commit dec010d

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

compat/mingw.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../strbuf.h"
66
#include "../run-command.h"
77
#include "../cache.h"
8+
#include "win32/lazyload.h"
89

910
#define HCAST(type, handle) ((type)(intptr_t)handle)
1011

@@ -1798,6 +1799,33 @@ int mingw_getpagesize(void)
17981799
return si.dwAllocationGranularity;
17991800
}
18001801

1802+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1803+
enum EXTENDED_NAME_FORMAT {
1804+
NameDisplay = 3,
1805+
NameUserPrincipal = 8
1806+
};
1807+
1808+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1809+
{
1810+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1811+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1812+
static wchar_t wbuffer[1024];
1813+
DWORD len;
1814+
1815+
if (!INIT_PROC_ADDR(GetUserNameExW))
1816+
return NULL;
1817+
1818+
len = ARRAY_SIZE(wbuffer);
1819+
if (GetUserNameExW(type, wbuffer, &len)) {
1820+
char *converted = xmalloc((len *= 3));
1821+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1822+
return converted;
1823+
free(converted);
1824+
}
1825+
1826+
return NULL;
1827+
}
1828+
18011829
struct passwd *getpwuid(int uid)
18021830
{
18031831
static unsigned initialized;
@@ -1816,7 +1844,9 @@ struct passwd *getpwuid(int uid)
18161844

18171845
p = xmalloc(sizeof(*p));
18181846
p->pw_name = user_name;
1819-
p->pw_gecos = "unknown";
1847+
p->pw_gecos = get_extended_user_info(NameDisplay);
1848+
if (!p->pw_gecos)
1849+
p->pw_gecos = "unknown";
18201850
p->pw_dir = NULL;
18211851

18221852
initialized = 1;

0 commit comments

Comments
 (0)