]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Change to real implementation, do not call mbtowc.
authorUlrich Drepper <drepper@redhat.com>
Wed, 19 Nov 1997 23:25:18 +0000 (23:25 +0000)
committerUlrich Drepper <drepper@redhat.com>
Wed, 19 Nov 1997 23:25:18 +0000 (23:25 +0000)
stdlib/mblen.c

index 120551c9a249d7031207072d3f34b31d628a9918..e43b076371bb4de4d6bb9d87f820c6c15d422e0a 100644 (file)
    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;
 }