From 422d2810b37d1ec8a12f967089d04039800c2b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Thu, 27 Feb 2014 09:44:28 +0100 Subject: [PATCH] unix, windows: make uv_cwd be consistent with uv_exepath Fixes #446 --- include/uv.h | 2 +- src/unix/core.c | 10 ++++------ src/win/util.c | 23 ++++++++++++++++++++--- test/test-cwd-and-chdir.c | 6 +++--- test/test-fs.c | 4 +++- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/include/uv.h b/include/uv.h index 49c2a43e..cf5dca50 100644 --- a/include/uv.h +++ b/include/uv.h @@ -2076,7 +2076,7 @@ UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst); UV_EXTERN int uv_exepath(char* buffer, size_t* size); /* Gets the current working directory */ -UV_EXTERN int uv_cwd(char* buffer, size_t size); +UV_EXTERN int uv_cwd(char* buffer, size_t* size); /* Changes the current working directory */ UV_EXTERN int uv_chdir(const char* dir); diff --git a/src/unix/core.c b/src/unix/core.c index a8430449..936d8950 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -589,16 +589,14 @@ ssize_t uv__recvmsg(int fd, struct msghdr* msg, int flags) { } -int uv_cwd(char* buffer, size_t size) { - if (buffer == NULL) +int uv_cwd(char* buffer, size_t* size) { + if (buffer == NULL || size == NULL) return -EINVAL; - if (size == 0) - return -EINVAL; - - if (getcwd(buffer, size) == NULL) + if (getcwd(buffer, *size) == NULL) return -errno; + *size = strlen(buffer); return 0; } diff --git a/src/win/util.c b/src/win/util.c index c9ca773d..464599b2 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -163,12 +163,12 @@ int uv_exepath(char* buffer, size_t* size_ptr) { } -int uv_cwd(char* buffer, size_t size) { +int uv_cwd(char* buffer, size_t* size) { DWORD utf16_len; WCHAR utf16_buffer[MAX_PATH]; int r; - if (buffer == NULL || size == 0) { + if (buffer == NULL || size == NULL) { return UV_EINVAL; } @@ -192,19 +192,36 @@ int uv_cwd(char* buffer, size_t size) { utf16_buffer[utf16_len] = L'\0'; } + /* Check how much space we need */ + r = WideCharToMultiByte(CP_UTF8, + 0, + utf16_buffer, + -1, + NULL, + 0, + NULL, + NULL); + if (r == 0) { + return uv_translate_sys_error(GetLastError()); + } else if (r > (int) *size) { + *size = r; + return UV_ENOBUFS; + } + /* Convert to UTF-8 */ r = WideCharToMultiByte(CP_UTF8, 0, utf16_buffer, -1, buffer, - size > INT_MAX ? INT_MAX : (int) size, + *size > INT_MAX ? INT_MAX : (int) *size, NULL, NULL); if (r == 0) { return uv_translate_sys_error(GetLastError()); } + *size = r; return 0; } diff --git a/test/test-cwd-and-chdir.c b/test/test-cwd-and-chdir.c index f1082ac4..6f617319 100644 --- a/test/test-cwd-and-chdir.c +++ b/test/test-cwd-and-chdir.c @@ -33,8 +33,8 @@ TEST_IMPL(cwd_and_chdir) { char* last_slash; int err; - size = sizeof(buffer_orig) / sizeof(buffer_orig[0]); - err = uv_cwd(buffer_orig, size); + size = sizeof(buffer_orig); + err = uv_cwd(buffer_orig, &size); ASSERT(err == 0); /* Remove trailing slash unless at a root directory. */ @@ -55,7 +55,7 @@ TEST_IMPL(cwd_and_chdir) { err = uv_chdir(buffer_orig); ASSERT(err == 0); - err = uv_cwd(buffer_new, size); + err = uv_cwd(buffer_new, &size); ASSERT(err == 0); ASSERT(strcmp(buffer_orig, buffer_new) == 0); diff --git a/test/test-fs.c b/test/test-fs.c index 940672f8..306ec6b0 100644 --- a/test/test-fs.c +++ b/test/test-fs.c @@ -1465,8 +1465,10 @@ TEST_IMPL(fs_symlink_dir) { #ifdef _WIN32 { static char src_path_buf[PATHMAX]; + size_t size; + size = sizeof(src_path_buf); strcpy(src_path_buf, "\\\\?\\"); - uv_cwd(src_path_buf + 4, sizeof(src_path_buf)); + uv_cwd(src_path_buf + 4, &size); strcat(src_path_buf, "\\test_dir\\"); test_dir = src_path_buf; }