Skip to content

Commit 2016f9a

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 63fa0df commit 2016f9a

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
@@ -1829,6 +1829,33 @@ int mingw_getpagesize(void)
18291829
return si.dwAllocationGranularity;
18301830
}
18311831

1832+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1833+
enum EXTENDED_NAME_FORMAT {
1834+
NameDisplay = 3,
1835+
NameUserPrincipal = 8
1836+
};
1837+
1838+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1839+
{
1840+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1841+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1842+
static wchar_t wbuffer[1024];
1843+
DWORD len;
1844+
1845+
if (!INIT_PROC_ADDR(GetUserNameExW))
1846+
return NULL;
1847+
1848+
len = ARRAY_SIZE(wbuffer);
1849+
if (GetUserNameExW(type, wbuffer, &len)) {
1850+
char *converted = xmalloc((len *= 3));
1851+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1852+
return converted;
1853+
free(converted);
1854+
}
1855+
1856+
return NULL;
1857+
}
1858+
18321859
struct passwd *getpwuid(int uid)
18331860
{
18341861
static unsigned initialized;
@@ -1847,7 +1874,9 @@ struct passwd *getpwuid(int uid)
18471874

18481875
p = xmalloc(sizeof(*p));
18491876
p->pw_name = user_name;
1850-
p->pw_gecos = "unknown";
1877+
p->pw_gecos = get_extended_user_info(NameDisplay);
1878+
if (!p->pw_gecos)
1879+
p->pw_gecos = "unknown";
18511880
p->pw_dir = NULL;
18521881

18531882
initialized = 1;

0 commit comments

Comments
 (0)