summaryrefslogtreecommitdiff
path: root/win32/win32.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-16 10:25:56 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-16 10:25:56 (GMT)
commit000482a1730757f1cf0ba7e8874decbaebb90a4a (patch)
tree9e434fa2825253d62a2f175957ec2c834bea4e36 /win32/win32.c
parent87acdae43dcfab5d28c85b10374be9faf6209076 (diff)
rb_w32_ugetcwd: UTF-8 version getcwd
* dir.c (rb_dir_getwd): convert from UTF-8. * win32/win32.c (w32_getcwd): codepage aware getcwd using GetCurrentDirectoryW. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58745 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'win32/win32.c')
-rw-r--r--win32/win32.c52
1 files changed, 35 insertions, 17 deletions
diff --git a/win32/win32.c b/win32/win32.c
index 8b18821..0296f2d 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -4655,43 +4655,61 @@ clock_getres(clockid_t clock_id, struct timespec *sp)
}
/* License: Ruby's */
-char *
-rb_w32_getcwd(char *buffer, int size)
+static char *
+w32_getcwd(char *buffer, int size, UINT cp)
{
- char *p = buffer;
- int len;
+ WCHAR *p;
+ int wlen, len;
- len = GetCurrentDirectory(0, NULL);
+ len = GetCurrentDirectoryW(0, NULL);
if (!len) {
errno = map_errno(GetLastError());
return NULL;
}
- if (p) {
+ if (buffer && size < len) {
+ errno = ERANGE;
+ return NULL;
+ }
+
+ p = ALLOCA_N(WCHAR, len);
+ if (!GetCurrentDirectoryW(len, p)) {
+ errno = map_errno(GetLastError());
+ return NULL;
+ }
+
+ wlen = translate_wchar(p, L'\\', L'/') - p + 1;
+ len = WideCharToMultiByte(cp, 0, p, wlen, NULL, 0, NULL, NULL);
+ if (buffer) {
if (size < len) {
errno = ERANGE;
return NULL;
}
}
else {
- p = malloc(len);
- size = len;
- if (!p) {
+ buffer = malloc(len);
+ if (!buffer) {
errno = ENOMEM;
return NULL;
}
}
+ WideCharToMultiByte(cp, 0, p, wlen, buffer, len, NULL, NULL);
- if (!GetCurrentDirectory(size, p)) {
- errno = map_errno(GetLastError());
- if (!buffer)
- free(p);
- return NULL;
- }
+ return buffer;
+}
- translate_char(p, '\\', '/', filecp());
+/* License: Ruby's */
+char *
+rb_w32_getcwd(char *buffer, int size)
+{
+ return w32_getcwd(buffer, size, filecp());
+}
- return p;
+/* License: Ruby's */
+char *
+rb_w32_ugetcwd(char *buffer, int size)
+{
+ return w32_getcwd(buffer, size, CP_UTF8);
}
/* License: Artistic or GPL */