Skip to content

Commit 80c7fb6

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 8fa8bfb commit 80c7fb6

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
@@ -1915,6 +1915,33 @@ int mingw_getpagesize(void)
19151915
return si.dwAllocationGranularity;
19161916
}
19171917

1918+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1919+
enum EXTENDED_NAME_FORMAT {
1920+
NameDisplay = 3,
1921+
NameUserPrincipal = 8
1922+
};
1923+
1924+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1925+
{
1926+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1927+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1928+
static wchar_t wbuffer[1024];
1929+
DWORD len;
1930+
1931+
if (!INIT_PROC_ADDR(GetUserNameExW))
1932+
return NULL;
1933+
1934+
len = ARRAY_SIZE(wbuffer);
1935+
if (GetUserNameExW(type, wbuffer, &len)) {
1936+
char *converted = xmalloc((len *= 3));
1937+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1938+
return converted;
1939+
free(converted);
1940+
}
1941+
1942+
return NULL;
1943+
}
1944+
19181945
struct passwd *getpwuid(int uid)
19191946
{
19201947
static unsigned initialized;
@@ -1933,7 +1960,9 @@ struct passwd *getpwuid(int uid)
19331960

19341961
p = xmalloc(sizeof(*p));
19351962
p->pw_name = user_name;
1936-
p->pw_gecos = "unknown";
1963+
p->pw_gecos = get_extended_user_info(NameDisplay);
1964+
if (!p->pw_gecos)
1965+
p->pw_gecos = "unknown";
19371966
p->pw_dir = NULL;
19381967

19391968
initialized = 1;

0 commit comments

Comments
 (0)