From 4676c9f97a7f67df5cc108eca7746acc3bae224b Mon Sep 17 00:00:00 2001 From: Manoj Ampalam Date: Sun, 16 Oct 2016 22:58:39 -0700 Subject: [PATCH] Unicode enabled on gethostname --- contrib/win32/win32compat/misc.c | 6 ------ contrib/win32/win32compat/socketio.c | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/contrib/win32/win32compat/misc.c b/contrib/win32/win32compat/misc.c index e56b8cb..855259e 100644 --- a/contrib/win32/win32compat/misc.c +++ b/contrib/win32/win32compat/misc.c @@ -119,12 +119,6 @@ w32_fopen_utf8(const char *path, const char *mode) { return f; } -/*TODO implement Unicode host name support in Windows*/ -int -w32_gethostname(char *name, size_t len) { - return gethostname(name, len); -} - wchar_t* utf8_to_utf16(const char *utf8) { diff --git a/contrib/win32/win32compat/socketio.c b/contrib/win32/win32compat/socketio.c index 4353138..9d9079c 100644 --- a/contrib/win32/win32compat/socketio.c +++ b/contrib/win32/win32compat/socketio.c @@ -34,6 +34,7 @@ #include #include "w32fd.h" #include +#include "inc\utf.h" #define INTERNAL_SEND_BUFFER_SIZE 70*1024 //70KB @@ -978,4 +979,24 @@ socketio_on_select(struct w32_io* pio, BOOL rd) { } } +} + +int +w32_gethostname(char *name_utf8, size_t len) { + wchar_t name_utf16[256]; + char* tmp_name_utf8 = NULL; + if (GetHostNameW(name_utf16, 256) == SOCKET_ERROR) { + errno = errno_from_WSALastError(); + return -1; + } + + if ((tmp_name_utf8 = utf16_to_utf8(name_utf16)) == NULL || + strlen(tmp_name_utf8) >= len) { + errno = EFAULT; //?? + return -1; + } + + memcpy(name_utf8, tmp_name_utf8, strlen(tmp_name_utf8) + 1); + free(tmp_name_utf8); + return 0; } \ No newline at end of file