Skip to content

Commit 96f58de

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 841933d commit 96f58de

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
@@ -1932,6 +1932,33 @@ int mingw_getpagesize(void)
19321932
return si.dwAllocationGranularity;
19331933
}
19341934

1935+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1936+
enum EXTENDED_NAME_FORMAT {
1937+
NameDisplay = 3,
1938+
NameUserPrincipal = 8
1939+
};
1940+
1941+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1942+
{
1943+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1944+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1945+
static wchar_t wbuffer[1024];
1946+
DWORD len;
1947+
1948+
if (!INIT_PROC_ADDR(GetUserNameExW))
1949+
return NULL;
1950+
1951+
len = ARRAY_SIZE(wbuffer);
1952+
if (GetUserNameExW(type, wbuffer, &len)) {
1953+
char *converted = xmalloc((len *= 3));
1954+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1955+
return converted;
1956+
free(converted);
1957+
}
1958+
1959+
return NULL;
1960+
}
1961+
19351962
struct passwd *getpwuid(int uid)
19361963
{
19371964
static unsigned initialized;
@@ -1950,7 +1977,9 @@ struct passwd *getpwuid(int uid)
19501977

19511978
p = xmalloc(sizeof(*p));
19521979
p->pw_name = user_name;
1953-
p->pw_gecos = "unknown";
1980+
p->pw_gecos = get_extended_user_info(NameDisplay);
1981+
if (!p->pw_gecos)
1982+
p->pw_gecos = "unknown";
19541983
p->pw_dir = NULL;
19551984

19561985
initialized = 1;

0 commit comments

Comments
 (0)