Skip to content

Commit c6db784

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 ac70f54 commit c6db784

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

compat/mingw.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,33 @@ int mingw_getpagesize(void)
17631763
return si.dwAllocationGranularity;
17641764
}
17651765

1766+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1767+
enum EXTENDED_NAME_FORMAT {
1768+
NameDisplay = 3,
1769+
NameUserPrincipal = 8
1770+
};
1771+
1772+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1773+
{
1774+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1775+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1776+
static wchar_t wbuffer[1024];
1777+
DWORD len;
1778+
1779+
if (!INIT_PROC_ADDR(GetUserNameExW))
1780+
return NULL;
1781+
1782+
len = ARRAY_SIZE(wbuffer);
1783+
if (GetUserNameExW(type, wbuffer, &len)) {
1784+
char *converted = xmalloc((len *= 3));
1785+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1786+
return converted;
1787+
free(converted);
1788+
}
1789+
1790+
return NULL;
1791+
}
1792+
17661793
struct passwd *getpwuid(int uid)
17671794
{
17681795
static unsigned initialized;
@@ -1781,7 +1808,9 @@ struct passwd *getpwuid(int uid)
17811808

17821809
p = xmalloc(sizeof(*p));
17831810
p->pw_name = user_name;
1784-
p->pw_gecos = "unknown";
1811+
p->pw_gecos = get_extended_user_info(NameDisplay);
1812+
if (!p->pw_gecos)
1813+
p->pw_gecos = "unknown";
17851814
p->pw_dir = NULL;
17861815

17871816
initialized = 1;

0 commit comments

Comments
 (0)