diff options
author | tjr <tjr@FreeBSD.org> | 2004-04-06 13:14:03 +0000 |
---|---|---|
committer | tjr <tjr@FreeBSD.org> | 2004-04-06 13:14:03 +0000 |
commit | 4212f9711ffee179c943b9f40b788b759a1ad9e6 (patch) | |
tree | 6779dc19a3df61ca545f851748ebd5f5de3a255e /lib/libc/locale/mblen.c | |
parent | fcc4fa555ff7fcd7b9e4327aa717b090a18361b5 (diff) | |
download | FreeBSD-src-4212f9711ffee179c943b9f40b788b759a1ad9e6.zip FreeBSD-src-4212f9711ffee179c943b9f40b788b759a1ad9e6.tar.gz |
Prepare to handle state-dependent encodings. This mainly involves not
taking shortcuts when it comes to storing and passing around conversion
states.
Diffstat (limited to 'lib/libc/locale/mblen.c')
-rw-r--r-- | lib/libc/locale/mblen.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/libc/locale/mblen.c b/lib/libc/locale/mblen.c index 5025115..8db5335 100644 --- a/lib/libc/locale/mblen.c +++ b/lib/libc/locale/mblen.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2002, 2003 Tim J. Robbins. + * Copyright (c) 2002-2004 Tim J. Robbins. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,15 +27,29 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#include <errno.h> +#include <limits.h> #include <stdlib.h> +#include <wchar.h> int mblen(const char *s, size_t n) { + static const mbstate_t initial; + static mbstate_t mbs; + size_t rval; - /* - * Calling mbtowc() is only legal because we don't support - * state-dependent encodings. - */ - return (mbtowc(NULL, s, n)); + if (s == NULL) { + /* No support for state dependent encodings. */ + mbs = initial; + return (0); + } + rval = mbrtowc(NULL, s, n, &mbs); + if (rval == (size_t)-1 || rval == (size_t)-2) + return (-1); + if (rval > INT_MAX) { + errno = ERANGE; + return (-1); + } + return ((int)rval); } |