Skip to content

Commit b57b444

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 48783ae commit b57b444

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
@@ -1826,6 +1826,33 @@ int mingw_getpagesize(void)
18261826
return si.dwAllocationGranularity;
18271827
}
18281828

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

18451872
p = xmalloc(sizeof(*p));
18461873
p->pw_name = user_name;
1847-
p->pw_gecos = "unknown";
1874+
p->pw_gecos = get_extended_user_info(NameDisplay);
1875+
if (!p->pw_gecos)
1876+
p->pw_gecos = "unknown";
18481877
p->pw_dir = NULL;
18491878

18501879
initialized = 1;

0 commit comments

Comments
 (0)