Skip to content

Commit 364c882

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 cb0f440 commit 364c882

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
@@ -1844,6 +1844,33 @@ int mingw_getpagesize(void)
18441844
return si.dwAllocationGranularity;
18451845
}
18461846

1847+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1848+
enum EXTENDED_NAME_FORMAT {
1849+
NameDisplay = 3,
1850+
NameUserPrincipal = 8
1851+
};
1852+
1853+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1854+
{
1855+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1856+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1857+
static wchar_t wbuffer[1024];
1858+
DWORD len;
1859+
1860+
if (!INIT_PROC_ADDR(GetUserNameExW))
1861+
return NULL;
1862+
1863+
len = ARRAY_SIZE(wbuffer);
1864+
if (GetUserNameExW(type, wbuffer, &len)) {
1865+
char *converted = xmalloc((len *= 3));
1866+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1867+
return converted;
1868+
free(converted);
1869+
}
1870+
1871+
return NULL;
1872+
}
1873+
18471874
struct passwd *getpwuid(int uid)
18481875
{
18491876
static unsigned initialized;
@@ -1862,7 +1889,9 @@ struct passwd *getpwuid(int uid)
18621889

18631890
p = xmalloc(sizeof(*p));
18641891
p->pw_name = user_name;
1865-
p->pw_gecos = "unknown";
1892+
p->pw_gecos = get_extended_user_info(NameDisplay);
1893+
if (!p->pw_gecos)
1894+
p->pw_gecos = "unknown";
18661895
p->pw_dir = NULL;
18671896

18681897
initialized = 1;

0 commit comments

Comments
 (0)