diff options
author | tjr <tjr@FreeBSD.org> | 2002-08-31 14:16:12 +0000 |
---|---|---|
committer | tjr <tjr@FreeBSD.org> | 2002-08-31 14:16:12 +0000 |
commit | ca719015df3d7025ed5dab68f072529d15e9bde1 (patch) | |
tree | ae8df1a0ce3a83d40f146b0d2bcd6befaa55ce7f /lib/libc/locale/mbstowcs.c | |
parent | 624d02ba22e582550b9128552e7d8ceba6c3748b (diff) | |
download | FreeBSD-src-ca719015df3d7025ed5dab68f072529d15e9bde1.zip FreeBSD-src-ca719015df3d7025ed5dab68f072529d15e9bde1.tar.gz |
Implement the XSI extension which allows the destination string to be
NULL, and returns the number of bytes that would be required to store
the result of the conversion without storing anything.
PR: 17694
Diffstat (limited to 'lib/libc/locale/mbstowcs.c')
-rw-r--r-- | lib/libc/locale/mbstowcs.c | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/libc/locale/mbstowcs.c b/lib/libc/locale/mbstowcs.c index 5c60a5b..b473f89 100644 --- a/lib/libc/locale/mbstowcs.c +++ b/lib/libc/locale/mbstowcs.c @@ -37,6 +37,7 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#include <errno.h> #include <stdlib.h> #include <limits.h> #include <stddef.h> @@ -50,18 +51,38 @@ mbstowcs(pwcs, s, n) { char const *e; int cnt = 0; + rune_t r; - if (!pwcs || !s) + if (!s) { + errno = EINVAL; return (-1); + } + if (pwcs == NULL) { + /* Convert and count only, do not store. */ + while ((r = sgetrune(s, MB_LEN_MAX, &e)) != _INVALID_RUNE && + r != 0) { + s = e; + cnt++; + } + if (r == _INVALID_RUNE) { + errno = EILSEQ; + return (-1); + } + } + + /* Convert, store and count characters. */ while (n-- > 0) { *pwcs = sgetrune(s, MB_LEN_MAX, &e); - if (*pwcs == _INVALID_RUNE) + if (*pwcs == _INVALID_RUNE) { + errno = EILSEQ; return (-1); + } if (*pwcs++ == 0) break; s = e; ++cnt; } + return (cnt); } |