aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2017-04-07 19:46:33 +0200
committerGitHub <noreply@github.com>2017-04-07 19:46:33 +0200
commit13352c00f1909d9296c5f276a3735f5e6f231b39 (patch)
treec0dc65954b3084b6d98185f7b84ce62f8b32d185 /src
parent1813076c448f1039db33e08e83b7f1f2011db0ee (diff)
downloadrneovim-13352c00f1909d9296c5f276a3735f5e6f231b39.tar.gz
rneovim-13352c00f1909d9296c5f276a3735f5e6f231b39.tar.bz2
rneovim-13352c00f1909d9296c5f276a3735f5e6f231b39.zip
win: os_get_hostname() #5416 (#6413)
Diffstat (limited to 'src')
-rw-r--r--src/nvim/mbyte.c51
-rw-r--r--src/nvim/os/env.c24
-rw-r--r--src/nvim/os/fs.c2
3 files changed, 47 insertions, 30 deletions
diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c
index d96848754c..460528b85f 100644
--- a/src/nvim/mbyte.c
+++ b/src/nvim/mbyte.c
@@ -1304,6 +1304,7 @@ static int utf_strnicmp(const char_u *s1, const char_u *s2, size_t n1,
# define CP_UTF8 65001 /* magic number from winnls.h */
#endif
+/// Reassigns `strw` to a new, allocated pointer to a UTF16 string.
int utf8_to_utf16(const char *str, WCHAR **strw)
FUNC_ATTR_NONNULL_ALL
{
@@ -1345,40 +1346,40 @@ int utf8_to_utf16(const char *str, WCHAR **strw)
return 0;
}
+/// Reassigns `str` to a new, allocated pointer to a UTF8 string.
int utf16_to_utf8(const WCHAR *strw, char **str)
FUNC_ATTR_NONNULL_ALL
{
// Compute the space required to store the string as UTF-8.
- ssize_t utf8_len = WideCharToMultiByte(CP_UTF8,
- 0,
- strw,
- -1,
- NULL,
- 0,
- NULL,
- NULL);
+ DWORD utf8_len = WideCharToMultiByte(CP_UTF8,
+ 0,
+ strw,
+ -1,
+ NULL,
+ 0,
+ NULL,
+ NULL);
if (utf8_len == 0) {
return GetLastError();
}
- ssize_t buf_sz = utf8_len * sizeof(char);
- char *buf = xmalloc(buf_sz);
- char *pos = buf;
+ *str = xmalloc(utf8_len);
- // Convert string to UTF-8.
- int r = WideCharToMultiByte(CP_UTF8,
- 0,
- strw,
- -1,
- pos,
- utf8_len,
- NULL,
- NULL);
- assert(r == utf8_len);
- if (r != utf8_len) {
- EMSG2("WideCharToMultiByte failed: %d", r);
- }
- *str = pos;
+ // Convert to UTF-8.
+ utf8_len = WideCharToMultiByte(CP_UTF8,
+ 0,
+ strw,
+ -1,
+ *str,
+ utf8_len,
+ NULL,
+ NULL);
+ if (utf8_len == 0) {
+ free(*str);
+ *str = NULL;
+ return GetLastError();
+ }
+ (*str)[utf8_len] = '\0';
return 0;
}
diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c
index 839e0d1b51..12c2da6152 100644
--- a/src/nvim/os/env.c
+++ b/src/nvim/os/env.c
@@ -118,7 +118,6 @@ char *os_getenvname_at_index(size_t index)
return name;
}
-
/// Get the process ID of the Neovim process.
///
/// @return the process ID.
@@ -145,10 +144,27 @@ void os_get_hostname(char *hostname, size_t size)
} else {
xstrlcpy(hostname, vutsname.nodename, size);
}
+#elif defined(WIN32)
+ WCHAR host_utf16[MAX_COMPUTERNAME_LENGTH + 1];
+ DWORD host_wsize = sizeof(host_utf16) / sizeof(host_utf16[0]);
+ if (GetComputerNameW(host_utf16, &host_wsize) == 0) {
+ *hostname = '\0';
+ DWORD err = GetLastError();
+ EMSG2("GetComputerNameW failed: %d", err);
+ return;
+ }
+ host_utf16[host_wsize] = '\0';
+
+ char *host_utf8;
+ int conversion_result = utf16_to_utf8(host_utf16, &host_utf8);
+ if (conversion_result != 0) {
+ EMSG2("utf16_to_utf8 failed: %d", conversion_result);
+ return;
+ }
+ xstrlcpy(hostname, host_utf8, size);
+ xfree(host_utf8);
#else
- // TODO(unknown): Implement this for windows.
- // See the implementation used in vim:
- // https://code.google.com/p/vim/source/browse/src/os_win32.c?r=6b69d8dde19e32909f4ee3a6337e6a2ecfbb6f72#2899
+ EMSG("os_get_hostname failed: missing uname()");
*hostname = '\0';
#endif
}
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c
index 3833a43f5f..c33e1140e8 100644
--- a/src/nvim/os/fs.c
+++ b/src/nvim/os/fs.c
@@ -1009,7 +1009,7 @@ char *os_resolve_shortcut(const char *fname)
WCHAR *p;
const int conversion_result = utf8_to_utf16(fname, &p);
if (conversion_result != 0) {
- EMSG2("utf8_to_utf16 failed: %s", uv_strerror(conversion_result));
+ EMSG2("utf8_to_utf16 failed: %d", conversion_result);
}
if (p != NULL) {