Skip to content

Commit 7be0e38

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 011f23d commit 7be0e38

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
@@ -1926,6 +1926,33 @@ int mingw_getpagesize(void)
19261926
return si.dwAllocationGranularity;
19271927
}
19281928

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

19451972
p = xmalloc(sizeof(*p));
19461973
p->pw_name = user_name;
1947-
p->pw_gecos = "unknown";
1974+
p->pw_gecos = get_extended_user_info(NameDisplay);
1975+
if (!p->pw_gecos)
1976+
p->pw_gecos = "unknown";
19481977
p->pw_dir = NULL;
19491978

19501979
initialized = 1;

0 commit comments

Comments
 (0)