Index: /trunk/src/emx/src/lib/libc.def
===================================================================
--- /trunk/src/emx/src/lib/libc.def	(revision 1711)
+++ /trunk/src/emx/src/lib/libc.def	(revision 1712)
@@ -1352,4 +1352,2 @@
     "___wctype" @1367
     "____wctype" @1368
-    "main" @1369
-    "_main" @1370
Index: /trunk/src/emx/src/lib/locale/locale_ctype.c
===================================================================
--- /trunk/src/emx/src/lib/locale/locale_ctype.c	(revision 1711)
+++ /trunk/src/emx/src/lib/locale/locale_ctype.c	(revision 1712)
@@ -249,7 +249,6 @@
 const __LIBC_LOCALEWCTYPE    __libc_GLocaleWCtype =
 {
-    .awcUpper =
-    {
-
+    .awcLower =
+    {
         0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
         0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
@@ -287,5 +286,4 @@
     .awcUpper =
     {
-
         0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
         0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
@@ -466,5 +464,5 @@
 
 
-#if 1 /* code for regenerating that table - link staticly! */
+#if 0 /* code for regenerating that table - link staticly! */
 
 #include <InnoTekLIBC/locale.h>
@@ -608,6 +606,6 @@
 
     /* Lower */
-    printf("    .awcUpper =\n"
-           "    {\n");
+    printf("    .awcLower =\n"
+           "    {");
     for (i = 0; i < sizeof(__libc_GLocaleWCtype.awcLower) / sizeof(__libc_GLocaleWCtype.awcLower[0]); i++)
     {
@@ -623,5 +621,5 @@
     /* Upper */
     printf("    .awcUpper =\n"
-           "    {\n");
+           "    {");
     for (i = 0; i < sizeof(__libc_GLocaleWCtype.awcUpper) / sizeof(__libc_GLocaleWCtype.awcUpper[0]); i++)
     {
@@ -637,5 +635,5 @@
     /* Type */
     printf("    .aufType =\n"
-           "    {\n");
+           "    {");
     for (i = 0; i < sizeof(__libc_GLocaleWCtype.aufType) / sizeof(__libc_GLocaleWCtype.aufType[0]); i++)
     {
Index: /trunk/src/emx/src/lib/locale/mb_libuni.c
===================================================================
--- /trunk/src/emx/src/lib/locale/mb_libuni.c	(revision 1711)
+++ /trunk/src/emx/src/lib/locale/mb_libuni.c	(revision 1712)
@@ -99,4 +99,5 @@
     __wchar_t wc;
     unsigned char uch = *(const unsigned char *)s;
+#ifndef NO_CONVERSION_TABLES
     if (    !pState->chState
         &&  (wc = __libc_GLocaleCtype.aucUnicode[uch]) != 0xffff)
@@ -104,6 +105,7 @@
         if (pwc != NULL)
             *pwc = wc;
-        return !wc;
-    }
+        return wc != 0;
+    }
+#endif /* !NO_CONVERSION_TABLES */
 
     /*
@@ -111,6 +113,7 @@
      * and restore the state before we try convert anything.
      */
-    if (!pwc)
-        pwc = &wc;                      /* The api crashes elsewise. */
+    wchar_t *pwcIn = &wc;
+    if (n >= 0x7fffffff)
+        n = 0x7ffffffe;                 /* UTF-8 decoder/encoder don't like -1, so let's play safe. */
     size_t  cbInLeft = n;
     size_t  cwcOutLeft = 1;
@@ -133,7 +136,11 @@
         if (!rc)
         {
-            rc = UniUconvToUcs(__libc_GLocaleCtype.uobj, (void **)(void *)&s, &cbInLeft, (UniChar **)&pwc, &cwcOutLeft, &cIgnore);
-            if (!rc)
-            {
+            rc = UniUconvToUcs(__libc_GLocaleCtype.uobj, (void **)(void *)&s, &cbInLeft, (UniChar **)&pwcIn, &cwcOutLeft, &cIgnore);
+            if (    !rc
+                ||  (rc == UCONV_E2BIG && !cwcOutLeft))
+            {
+                if (pwc)
+                    *pwc = wc;
+                rc = 0;
                 if (!UniQueryUconvObject(__libc_GLocaleCtype.uobj, &attr, sizeof(attr), NULL, NULL, NULL))
                     pState->chState = attr.state;
@@ -145,5 +152,5 @@
 
     if (!rc)
-        return n - cbInLeft;
+        return n - cbInLeft - !wc;
     /* failure */
     libuni_errno(rc);
@@ -170,4 +177,5 @@
      */
     char    ch;
+#ifndef NO_CONVERSION_TABLES
     if (!pState->chState)
     {
@@ -204,4 +212,5 @@
         /* not found */
     }
+#endif /* !NO_CONVERSION_TABLES */
 
     /*
@@ -279,5 +288,5 @@
 {
     const char *pch = *src;
-    size_t      cwc = 0;
+    size_t      cbIn = nms;
     if (dst == NULL)
     {
@@ -291,5 +300,5 @@
                     return (size_t)-1;    /* Invalid sequence - mbrtowc() sets errno. */
                 else /* if (cb == 0 || cb == (size_t)-2) */
-                    return cwc;
+                    return cbIn - nms;
             }
 
@@ -297,5 +306,4 @@
             pch += cb;
             nms -= cb;
-            cwc++;
         }
         /* (won't ever get here.) */
@@ -311,10 +319,10 @@
                 {
                     *src = NULL;
-                    return cwc;
+                    return cbIn - nms;
                 }
                 else if (cb == (size_t)-2)
                 {
                     *src = pch + nms;
-                    return cwc;
+                    return cbIn - nms;
                 }
                 else /*if (cb == (size_t)-1) */
@@ -328,10 +336,9 @@
             pch += cb;
             nms -= cb;
-            cwc++;
             dst++;
         }
 
         *src = pch;
-        return cwc;
+        return cbIn - nms;
     }
 }
Index: /trunk/src/emx/src/lib/locale/setlocale.c
===================================================================
--- /trunk/src/emx/src/lib/locale/setlocale.c	(revision 1711)
+++ /trunk/src/emx/src/lib/locale/setlocale.c	(revision 1712)
@@ -206,5 +206,5 @@
 static const char *getDefaultLocale(const char *pszCategory, char *pszBuffer, int *pfDefault);
 static int getCodepage(const char *pszCodepage, const char *pszLocale, LocaleObject lobj, UniChar *pucsCodepage, unsigned cucCodepage);
-static int query_mbcs(UconvObject uobj, char *mbcs, unsigned char *au2MBCSPrefixs, int *pmb_cur_max);
+static int query_mbcs(UconvObject uobj, char *mbcs, unsigned char *au2MBCSPrefixs);
 
 static int localeCollateDo(__LIBC_PLOCALECOLLATE pCollate, UconvObject uobj, LocaleObject lobj);
@@ -336,5 +336,5 @@
 
 
-static int query_mbcs(UconvObject uobj, char *mbcs, unsigned char *au2MBCSPrefixs, int *pmb_cur_max)
+static int query_mbcs(UconvObject uobj, char *mbcs, unsigned char *au2MBCSPrefixs)
 {
     unsigned            i;
@@ -353,7 +353,5 @@
      * Create the return values.
      */
-    if (pmb_cur_max)
-        *pmb_cur_max = uconv_attr.mb_max_len;
-    *mbcs = (uconv_attr.mb_max_len > 1);
+    *mbcs = uconv_attr.mb_max_len > 1 ? uconv_attr.mb_max_len : 0;
 
     bzero(au2MBCSPrefixs, 256/4);
@@ -384,5 +382,5 @@
      * Query multi-byte related stuff.
      */
-    rc = query_mbcs(uobj, &pCollate->mbcs, &pCollate->au2MBCSPrefixs[0], NULL);
+    rc = query_mbcs(uobj, &pCollate->mbcs, &pCollate->au2MBCSPrefixs[0]);
     if (rc)
         return rc;
@@ -508,5 +506,5 @@
      * Query multi-byte related stuff.
      */
-    rc = query_mbcs(uobj, &pCtype->mbcs, &pCtype->au2MBCSPrefixs[0], NULL);
+    rc = query_mbcs(uobj, &pCtype->mbcs, &pCtype->au2MBCSPrefixs[0]);
     if (rc)
         return rc;
@@ -1446,4 +1444,5 @@
         localeCtypeFree(&__libc_GLocaleCtype);
         memcpy(&__libc_GLocaleCtype, &pTemp->Ctype, sizeof(__libc_GLocaleCtype));
+        MB_CUR_MAX = pTemp->Ctype.mbcs ? pTemp->Ctype.mbcs : 1;
         pTemp->afProcessed[LC_CTYPE + 1] = 0;
         gLocale.apszNames[LC_CTYPE + 1] = pTemp->Global.apszNames[LC_CTYPE + 1];
