From 007cfe8a0ca4c03412f031878d6ce232612a90d8 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Mon, 21 Dec 2020 10:06:11 +0100 Subject: w32: Allow Unicode for internal getusername function. * src/sysutils.c (_gpgrt_getusername) [W32]: Use GetUserNameW. -- This is used by argparse for user specific parts. Tested using the echo meta-command. Signed-off-by: Werner Koch --- src/sysutils.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/sysutils.c b/src/sysutils.c index 8005a51..ebd587c 100644 --- a/src/sysutils.c +++ b/src/sysutils.c @@ -482,17 +482,27 @@ _gpgrt_getusername (void) char *result = NULL; #ifdef HAVE_W32_SYSTEM - char tmp[1]; - DWORD size = 1; - - /* FIXME: We need to support utf8 */ - GetUserNameA (tmp, &size); - result = _gpgrt_malloc (size); - if (result && !GetUserNameA (result, &size)) + wchar_t wtmp[1]; + wchar_t *wbuf; + DWORD wsize = 1; + char *buf; + + GetUserNameW (wtmp, &wsize); + wbuf = _gpgrt_malloc (wsize * sizeof *wbuf); + if (!wbuf) + { + _gpgrt_w32_set_errno (-1); + return NULL; + } + if (!GetUserNameW (wbuf, &wsize)) { - xfree (result); - result = NULL; + _gpgrt_w32_set_errno (-1); + xfree (wbuf); + return NULL; } + buf = _gpgrt_wchar_to_utf8 (wbuf, wsize); + xfree (wbuf); + return buf; #else /* !HAVE_W32_SYSTEM */ -- cgit v1.2.3