Print this page
2964 need POSIX 2008 locale object support
Reviewed by: Robert Mustacchi <rm@joyent.com>

@@ -1,6 +1,7 @@
 /*
+ * Copyright 2013 Garrett D'Amore <garrett@damore.org>
  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
  * Copyright (c) 2002-2004 Tim J. Robbins.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without

@@ -27,28 +28,39 @@
 
 #include "lint.h"
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
+#include <locale.h>
 #include <wchar.h>
 #include "mblocal.h"
+#include "localeimpl.h"
+#include "lctype.h"
 
 size_t
-wcsnrtombs(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
-    size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD ps)
+wcsnrtombs_l(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
+    size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD ps, locale_t loc)
 {
         static mbstate_t mbs;
 
         if (ps == NULL)
                 ps = &mbs;
-        return (__wcsnrtombs(dst, src, nwc, len, ps));
+        return (loc->ctype->lc_wcsnrtombs(dst, src, nwc, len, ps));
 }
 
 size_t
-__wcsnrtombs_std(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
+wcsnrtombs(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD ps)
 {
+        return (wcsnrtombs_l(dst, src, nwc, len, ps, uselocale(NULL)));
+}
+
+size_t
+__wcsnrtombs_std(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
+    size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD ps,
+    wcrtomb_pfn_t pwcrtomb)
+{
         mbstate_t mbsbak;
         char buf[MB_LEN_MAX];
         const wchar_t *s;
         size_t nbytes;
         size_t nb;

@@ -56,11 +68,11 @@
         s = *src;
         nbytes = 0;
 
         if (dst == NULL) {
                 while (nwc-- > 0) {
-                        if ((nb = __wcrtomb(buf, *s, ps)) == (size_t)-1)
+                        if ((nb = pwcrtomb(buf, *s, ps)) == (size_t)-1)
                                 /* Invalid character - wcrtomb() sets errno. */
                                 return ((size_t)-1);
                         else if (*s == L'\0')
                                 return (nbytes + nb - 1);
                         s++;

@@ -70,11 +82,11 @@
         }
 
         while (len > 0 && nwc-- > 0) {
                 if (len > (size_t)MB_CUR_MAX) {
                         /* Enough space to translate in-place. */
-                        if ((nb = __wcrtomb(dst, *s, ps)) == (size_t)-1) {
+                        if ((nb = pwcrtomb(dst, *s, ps)) == (size_t)-1) {
                                 *src = s;
                                 return ((size_t)-1);
                         }
                 } else {
                         /*

@@ -83,11 +95,11 @@
                          * We need to save a copy of the conversion state
                          * here so we can restore it if the multibyte
                          * character is too long for the buffer.
                          */
                         mbsbak = *ps;
-                        if ((nb = __wcrtomb(buf, *s, ps)) == (size_t)-1) {
+                        if ((nb = pwcrtomb(buf, *s, ps)) == (size_t)-1) {
                                 *src = s;
                                 return ((size_t)-1);
                         }
                         if (nb > (int)len) {
                                 /* MB sequence for character won't fit. */