Implement wctomb(3) for ltrace.

This is an implementation in the style of the rest: char == byte.

We might want to come back and implement UTF-8, but this is enough for ltrace.

Bug: 13747066
Change-Id: Ib2b63609c9014fdef9a8491e067467c4fc5ae3cc
diff --git a/libc/bionic/wchar.cpp b/libc/bionic/wchar.cpp
index 50a3875..8d56458 100644
--- a/libc/bionic/wchar.cpp
+++ b/libc/bionic/wchar.cpp
@@ -28,6 +28,7 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <string.h>
 #include <wchar.h>
@@ -156,8 +157,8 @@
   return 1;
 }
 
-size_t mbrlen(const char* /*s*/, size_t n, mbstate_t* /*ps*/) {
-  return (n != 0);
+size_t mbrlen(const char* s, size_t n, mbstate_t* /*ps*/) {
+  return (n == 0 || s[0] == 0) ? 0 : 1;
 }
 
 size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* /*ps*/) {
@@ -217,13 +218,26 @@
   return ungetc(static_cast<char>(wc), stream);
 }
 
-size_t wcrtomb(char* s, wchar_t /*wc*/, mbstate_t* /*ps*/) {
-  if (s != NULL) {
-    *s = 1;
+int wctomb(char* s, wchar_t wc) {
+  if (s == NULL) {
+    return 0;
+  }
+  if (wc <= 0xff) {
+    *s = static_cast<char>(wc);
+  } else {
+    *s = '?';
   }
   return 1;
 }
 
+size_t wcrtomb(char* s, wchar_t wc, mbstate_t* /*ps*/) {
+  if (s == NULL) {
+    char buf[MB_LEN_MAX];
+    return wctomb(buf, L'\0');
+  }
+  return wctomb(s, wc);
+}
+
 size_t wcsftime(wchar_t* wcs, size_t maxsize, const wchar_t* format,  const struct tm* timptr) {
   return strftime(reinterpret_cast<char*>(wcs), maxsize, reinterpret_cast<const char*>(format), timptr);
 }