diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/iobuf.c | 45 | ||||
-rw-r--r-- | common/sysutils.c | 36 | ||||
-rw-r--r-- | common/sysutils.h | 1 |
3 files changed, 67 insertions, 15 deletions
diff --git a/common/iobuf.c b/common/iobuf.c index b20761ba1..4fd8cf57d 100644 --- a/common/iobuf.c +++ b/common/iobuf.c @@ -193,6 +193,19 @@ fd_cache_strcmp (const char *a, const char *b) #endif } + +#ifdef HAVE_W32_SYSTEM +static int +any8bitchar (const char *string) +{ + if (string) + for ( ; *string; string++) + if ((*string & 0x80)) + return 1; + return 0; +} +#endif /*HAVE_W32_SYSTEM*/ + /* * Invalidate (i.e. close) a cached iobuf */ @@ -294,21 +307,23 @@ direct_open (const char *fname, const char *mode, int mode700) sm = FILE_SHARE_READ; } -#ifdef HAVE_W32CE_SYSTEM - { - wchar_t *wfname = utf8_to_wchar (fname); - if (wfname) - { - hfile = CreateFile (wfname, da, sm, NULL, cd, - FILE_ATTRIBUTE_NORMAL, NULL); - xfree (wfname); - } - else - hfile = INVALID_HANDLE_VALUE; - } -#else - hfile = CreateFile (fname, da, sm, NULL, cd, FILE_ATTRIBUTE_NORMAL, NULL); -#endif + /* We use the Unicode version of the function only if needed to + * avoid an extra conversion step. */ + if (any8bitchar (fname)) + { + wchar_t *wfname = utf8_to_wchar (fname); + if (wfname) + { + hfile = CreateFileW (wfname, da, sm, NULL, cd, + FILE_ATTRIBUTE_NORMAL, NULL); + xfree (wfname); + } + else + hfile = INVALID_HANDLE_VALUE; + } + else + hfile = CreateFileA (fname, da, sm, NULL, cd, FILE_ATTRIBUTE_NORMAL, NULL); + return hfile; #else /*!HAVE_W32_SYSTEM*/ diff --git a/common/sysutils.c b/common/sysutils.c index 7b2f70311..e06be057f 100644 --- a/common/sysutils.c +++ b/common/sysutils.c @@ -171,6 +171,18 @@ enable_core_dumps (void) #endif } +#ifdef HAVE_W32_SYSTEM +static int +any8bitchar (const char *string) +{ + if (string) + for ( ; *string; string++) + if ((*string & 0x80)) + return 1; + return 0; +} +#endif /*HAVE_W32_SYSTEM*/ + /* Allow the use of special "-&nnn" style file names. */ void @@ -1095,6 +1107,30 @@ gnupg_access (const char *name, int mode) #endif } +/* A wrapper around open to handle Unicode file names under Windows. */ +int +gnupg_open (const char *name, int flags, unsigned int mode) +{ +#ifdef HAVE_W32_SYSTEM + if (any8bitchar (name)) + { + wchar_t *wname; + int ret; + + wname = utf8_to_wchar (name); + if (!wname) + return -1; + ret = _wopen (wname, flags, mode); + xfree (wname); + return ret; + } + else + return open (name, flags, mode); +#else + return open (name, flags, mode); +#endif +} + #ifdef HAVE_W32CE_SYSTEM diff --git a/common/sysutils.h b/common/sysutils.h index 1970f0a26..aabd15f8b 100644 --- a/common/sysutils.h +++ b/common/sysutils.h @@ -73,6 +73,7 @@ int gnupg_setenv (const char *name, const char *value, int overwrite); int gnupg_unsetenv (const char *name); char *gnupg_getcwd (void); gpg_err_code_t gnupg_access (const char *name, int mode); +int gnupg_open (const char *name, int flags, unsigned int mode); char *gnupg_get_socket_name (int fd); int gnupg_fd_valid (int fd); |