diff --git a/winsup/cygwin/msys2_path_conv.cc b/winsup/cygwin/msys2_path_conv.cc index f9dcda5d63..54f58fbe8e 100644 --- a/winsup/cygwin/msys2_path_conv.cc +++ b/winsup/cygwin/msys2_path_conv.cc @@ -624,7 +624,7 @@ void posix_to_win32_path(const char* from, const char* to, char** dst, const cha strncpy(one_path, from, to-from); one_path[to-from] = '\0'; - path_conv conv (one_path, 0); + path_conv conv (one_path, PC_KEEP_FINAL_SLASH); if (conv.error) { set_errno(conv.error); diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc index c4f74fb5fa..9a2c05b492 100644 --- a/winsup/cygwin/path.cc +++ b/winsup/cygwin/path.cc @@ -1254,9 +1254,22 @@ path_conv::check (const char *src, unsigned opt, cfree (wide_path); wide_path = NULL; } + + if (need_directory) + { + size_t n = strlen (this->path); + /* Do not add trailing \ to UNC device names like \\.\a: */ + if (this->path[n - 1] != '\\' && + (strncmp (this->path, "\\\\.\\", 4) != 0)) + { + this->modifiable_path ()[n] = '\\'; + this->modifiable_path ()[n + 1] = '\0'; + } + need_directory = 0; + } } - if (need_directory) + if ((opt & PC_KEEP_FINAL_SLASH) && need_directory) { size_t n = strlen (this->path); /* Do not add trailing \ to UNC device names like \\.\a: */ @@ -1266,6 +1279,7 @@ path_conv::check (const char *src, unsigned opt, this->modifiable_path ()[n] = '\\'; this->modifiable_path ()[n + 1] = '\0'; } + need_directory = 0; } if (opt & PC_OPEN) diff --git a/winsup/cygwin/path.h b/winsup/cygwin/path.h index adb0ca11f5..208147f4fc 100644 --- a/winsup/cygwin/path.h +++ b/winsup/cygwin/path.h @@ -59,6 +59,7 @@ enum pathconv_arg PC_KEEP_HANDLE = _BIT (12), /* keep handle for later stat calls */ PC_NO_ACCESS_CHECK = _BIT (13), /* helper flag for error check */ PC_SYM_NOFOLLOW_DIR = _BIT (14), /* don't follow a trailing slash */ + PC_KEEP_FINAL_SLASH = _BIT (15), /* do not remove a trailing slash */ PC_DONT_USE = _BIT (31) /* conversion to signed happens. */ };