Skip to content

Commit 54792ad

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 b10161d commit 54792ad

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

compat/mingw.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../strbuf.h"
66
#include "../run-command.h"
77
#include "../cache.h"
8+
#include "win32/lazyload.h"
89

910
#define HCAST(type, handle) ((type)(intptr_t)handle)
1011

@@ -1768,6 +1769,33 @@ int mingw_getpagesize(void)
17681769
return si.dwAllocationGranularity;
17691770
}
17701771

1772+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1773+
enum EXTENDED_NAME_FORMAT {
1774+
NameDisplay = 3,
1775+
NameUserPrincipal = 8
1776+
};
1777+
1778+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1779+
{
1780+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1781+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1782+
static wchar_t wbuffer[1024];
1783+
DWORD len;
1784+
1785+
if (!INIT_PROC_ADDR(GetUserNameExW))
1786+
return NULL;
1787+
1788+
len = ARRAY_SIZE(wbuffer);
1789+
if (GetUserNameExW(type, wbuffer, &len)) {
1790+
char *converted = xmalloc((len *= 3));
1791+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1792+
return converted;
1793+
free(converted);
1794+
}
1795+
1796+
return NULL;
1797+
}
1798+
17711799
struct passwd *getpwuid(int uid)
17721800
{
17731801
static unsigned initialized;
@@ -1786,7 +1814,9 @@ struct passwd *getpwuid(int uid)
17861814

17871815
p = xmalloc(sizeof(*p));
17881816
p->pw_name = user_name;
1789-
p->pw_gecos = "unknown";
1817+
p->pw_gecos = get_extended_user_info(NameDisplay);
1818+
if (!p->pw_gecos)
1819+
p->pw_gecos = "unknown";
17901820
p->pw_dir = NULL;
17911821

17921822
initialized = 1;

0 commit comments

Comments
 (0)