hi, there!

sorry for posting here but freebsd-i18n list seems to be dead (is it?)

some products (e.g. Xerces for C from XML Apache Project) rely on the
following behaviour of mbstowcs/wcstombs:

when `dest' param is NULL then `len' parameter is ignored and
these functions return required length to store the result.
This feature is implemented at least in Solaris' libc and glibc 2.1.2.

attached patch implements this behaviour (with updated man page and
one minor fix in man 3 multibyte).

btw is there ongoing effort to merge Citrus libxpg4 to our base tree?
freebsd-i18n archives are empty. searching yields nothing
except links to Citrus homepage and NetBSD `whatsnew' pages.

/fjoe
--- lib/libc/locale/ansi.c.orig Mon Jul  3 09:58:32 2000
+++ lib/libc/locale/ansi.c      Mon Jul  3 10:05:29 2000
@@ -105,15 +105,21 @@
        char const *e;
        int cnt = 0;
 
-       if (!pwcs || !s)
+       if (!s)
                return (-1);
 
-       while (n-- > 0) {
-               *pwcs = sgetrune(s, MB_LEN_MAX, &e);
-               if (*pwcs == _INVALID_RUNE)
+       while (!pwcs || n-- > 0) {
+               wchar_t wc;
+
+               wc = sgetrune(s, MB_LEN_MAX, &e);
+               if (pwcs)
+                       *pwcs = wc;
+               if (wc == _INVALID_RUNE)
                        return (-1);
-               if (*pwcs++ == 0)
+               if (wc == 0)
                        break;
+               if (pwcs)
+                       pwcs++;
                s = e;
                ++cnt;
        }
@@ -129,23 +135,30 @@
        char *e;
        int cnt, nb;
 
-       if (!pwcs || !s || n > INT_MAX)
+       if (!pwcs || (s && n > INT_MAX))
                return (-1);
 
        nb = n;
        cnt = 0;
-       while (nb > 0) {
+       while (!s || nb > 0) {
+               char c[MB_LEN_MAX + 1];
+               size_t c_cnt;
+
                if (*pwcs == 0) {
-                       *s = 0;
+                       if (s)
+                               *s = 0;
                        break;
                }
-               if (!sputrune(*pwcs++, s, nb, &e))
+               if (!sputrune(*pwcs++, s ? s : c, s ? nb : MB_LEN_MAX, &e))
                        return (-1);            /* encoding error */
                if (!e)                 /* too long */
                        return (cnt);
-               cnt += e - s;
-               nb -= e - s;
-               s = e;
+               c_cnt = s ? e - s : e - c;
+               cnt += c_cnt;
+               if (s) {
+                       nb -= c_cnt;
+                       s += c_cnt;
+               }
        }
        return (cnt);
 }
--- lib/libc/locale/multibyte.3.orig    Mon Jul  3 10:08:00 2000
+++ lib/libc/locale/multibyte.3 Mon Jul  3 10:20:50 2000
@@ -77,7 +77,7 @@
 and code each basic element as a sequence of C
 .Va char Ns s .
 Individual basic elements may map into one or more
-.Pq up to Dv MB_CHAR_MAX
+.Pq up to Dv MB_LEN_MAX
 bytes in a multibyte character.
 .Pp
 The current locale
@@ -176,6 +176,14 @@
 .Fa nwchars
 wide characters are stored.
 A terminating null wide character is appended if there is room.
+If
+.Fa wcstring
+is a null pointer,
+.Fn mbstowcs
+returns the length required to convert the entire array regardless
+of the value of
+.Fa nwchars ,
+but no values are stored.
 .Pp
 The
 .Fn wcstombs
@@ -189,6 +197,14 @@
 .Fa mbstring .
 Partial multibyte characters at the end of the string are not stored.
 The multibyte character string is null terminated if there is room.
+If
+.Fa mbstring
+is a null pointer,
+.Fn wcstombs
+returns the length required to convert the entire array regardless
+of the value of
+.Fa nbytes ,
+but no values are stored.
 .Sh "RETURN VALUES
 If multibyte characters are not supported in the current locale,
 all of these functions will return \-1 if characters can be processed,
@@ -216,11 +232,15 @@
 .Pp
 The
 .Fn mbstowcs
-function returns the number of wide characters converted,
+function returns the number of wide characters converted (or required if
+.Fa wcstring
+is NULL),
 not counting any terminating null wide character.
 The
 .Fn wcstombs
-function returns the number of bytes converted,
+function returns the number of bytes converted (or required if
+.Fa mbstring
+is NULL),
 not counting any terminating null byte.
 If any invalid multibyte characters are encountered,
 both functions return \-1.

Reply via email to