w32: Support utf8 for getcwd even if build with gpgrt < 1.40.

* common/sysutils.c (gnupg_getcwd) [W32]: Use Unicode version.
--

gpgrt 1.40 has not yet been released, so to make getcwd work properly
on Windows we need to have the code here as well.

GnuPG-bug-id: 5098
Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2020-11-10 10:04:16 +01:00
parent 0ca0a30bd6
commit 9188a3c6b7
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
1 changed files with 28 additions and 8 deletions

View File

@ -1100,9 +1100,33 @@ char *
gnupg_getcwd (void)
{
#if GPGRT_VERSION_NUMBER < 0x012800 /* 1.40 */
/* We use the old code which is okay despite that it does not
* support Unicode on Windows. For Windows this doesn't matter
* because we use the latest gpgrt anyway. */
# ifdef HAVE_W32_SYSTEM
wchar_t wbuffer[MAX_PATH + sizeof(wchar_t)];
DWORD wlen;
char *buf, *p;
wlen = GetCurrentDirectoryW (MAX_PATH, wbuffer);
if (!wlen)
{
gpg_err_set_errno (EINVAL);
return NULL;
}
else if (wlen > MAX_PATH)
{
gpg_err_set_errno (ENAMETOOLONG);
return NULL;
}
buf = wchar_to_utf8 (wbuffer);
if (buf)
{
for (p=buf; *p; p++)
if (*p == '\\')
*p = '/';
}
return buf;
# else /*Unix*/
char *buffer;
size_t size = 100;
@ -1111,18 +1135,14 @@ gnupg_getcwd (void)
buffer = xtrymalloc (size+1);
if (!buffer)
return NULL;
# ifdef HAVE_W32CE_SYSTEM
strcpy (buffer, "/"); /* Always "/". */
return buffer;
# else
if (getcwd (buffer, size) == buffer)
return buffer;
xfree (buffer);
if (errno != ERANGE)
return NULL;
size *= 2;
# endif
}
# endif /*Unix*/
#else
return gpgrt_getcwd ();
#endif