summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cygwin/cygwin.c22
1 files changed, 5 insertions, 17 deletions
diff --git a/cygwin/cygwin.c b/cygwin/cygwin.c
index afdb27b315..da62c95162 100644
--- a/cygwin/cygwin.c
+++ b/cygwin/cygwin.c
@@ -163,26 +163,14 @@ char*
wide_to_utf8(const wchar_t *wbuf)
{
dTHX;
- char *buf;
- int wlen = 0;
- char *oldlocale;
-
- /* Here and elsewhere in this file, we have a critical section to prevent
- * another thread from changing the locale out from under us. XXX But why
- * not just use uvchr_to_utf8? */
- SETLOCALE_LOCK;
+ const Size_t wlen = (wcslen(wbuf) + 1) * sizeof(wchar_t);
- oldlocale = setlocale(LC_CTYPE, NULL);
- setlocale(LC_CTYPE, "utf-8");
+ /* Max expansion factor is 3/2 */
+ Size_t blen = wlen * 3 / 2;
- wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
- buf = (char *) safemalloc(wlen+1);
- wcsrtombs(buf, (const wchar_t **)&wbuf, wlen, NULL);
+ char *buf = (char *) safemalloc(blen);
- if (oldlocale) setlocale(LC_CTYPE, oldlocale);
- else setlocale(LC_CTYPE, "C");
-
- SETLOCALE_UNLOCK;
+ utf16_to_utf8((U8 *) wbuf, buf, wlen, &blen);
return buf;
}