about summary refs log tree commit diff
path: root/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/mblen.c31
-rw-r--r--stdlib/stdlib.h8
2 files changed, 27 insertions, 12 deletions
diff --git a/stdlib/mblen.c b/stdlib/mblen.c
index 120551c9a2..e43b076371 100644
--- a/stdlib/mblen.c
+++ b/stdlib/mblen.c
@@ -17,14 +17,37 @@
    Boston, MA 02111-1307, USA.  */
 
 #include <stdlib.h>
-
-#undef	mblen
+#include <wchar.h>
 
 
 /* Return the length of the multibyte character (if there is one)
-   at S which is no longer than N characters.  */
+   at S which is no longer than N characters.
+   The ISO C standard says that the `mblen' function must not change
+   the global state.  */
 int
 mblen (const char *s, size_t n)
 {
-  return mbtowc ((wchar_t *) NULL, s, n);
+  mbstate_t state;
+  int result;
+
+  /* If S is NULL the function has to return null or not null
+     depending on the encoding having a state depending encoding or
+     not.  This is nonsense because any multibyte encoding has a
+     state.  The ISO C amendment 1 corrects this while introducing the
+     restartable functions.  We simply say here all encodings have a
+     state.  */
+  if (s == NULL)
+    return 1;
+
+  state.count = 0;
+  state.value = 0;
+
+  result = __mbrtowc (NULL, s, n, &state);
+
+  /* The `mbrtowc' functions tell us more than we need.  Fold the -1
+     and -2 result into -1.  */
+  if (result < 0)
+    result = -1;
+
+  return result;
 }
diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
index 2adefb508f..4bf3105269 100644
--- a/stdlib/stdlib.h
+++ b/stdlib/stdlib.h
@@ -686,14 +686,6 @@ extern int mbtowc __P ((wchar_t *__restrict __pwc,
    by WCHAR in S, returning its length.  */
 extern int wctomb __P ((char *__s, wchar_t __wchar));
 
-#if defined __OPTIMIZE__ && __GNUC__ >= 2
-extern __inline int
-mblen (__const char *__s, size_t __n)
-{
-  return mbtowc ((wchar_t *) NULL, __s, __n);
-}
-#endif /* Optimizing GCC >=2.  */
-
 
 /* Convert a multibyte string to a wide char string.  */
 extern size_t mbstowcs __P ((wchar_t *__restrict  __pwcs,