Skip to content

Commit febbf68

Browse files
author
Muiez Ahmed
committed
[SystemZ][z/OS] Missing wchar functions libc++
The aim is to add the missing z/OS specific implementations for mbsnrtowcs and wcsnrtombs, as part of libc++. Differential Revision: https://reviews.llvm.org/D98207
1 parent d7cd208 commit febbf68

File tree

4 files changed

+167
-3
lines changed

4 files changed

+167
-3
lines changed

libcxx/include/wchar.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_PREFERRED_OVERLOAD
170170
}
171171
#endif
172172

173-
#if defined(__cplusplus) && defined(_LIBCPP_MSVCRT_LIKE)
173+
#if defined(__cplusplus) && (defined(_LIBCPP_MSVCRT_LIKE) || defined(__MVS__))
174174
extern "C" {
175175
size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
176176
size_t nmc, size_t len, mbstate_t *__restrict ps);
177177
size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
178178
size_t nwc, size_t len, mbstate_t *__restrict ps);
179-
} // extern "C++"
180-
#endif // __cplusplus && _LIBCPP_MSVCRT
179+
} // extern "C"
180+
#endif // __cplusplus && (_LIBCPP_MSVCRT || __MVS__)
181181

182182
#endif // _LIBCPP_WCHAR_H

libcxx/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
9292
)
9393
elseif(ZOS)
9494
list(APPEND LIBCXX_SOURCES
95+
support/ibm/mbsnrtowcs.inc
96+
support/ibm/wcsnrtombs.inc
9597
support/ibm/xlocale_zos.cpp
9698
)
9799
endif()

libcxx/src/support/ibm/mbsnrtowcs.inc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*-
2+
* Some portions of this implementation are copied from
3+
* FreeBSD libc. These are covered by the following copyright:
4+
*
5+
* Copyright (c) 2002-2004 Tim J. Robbins.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26+
* SUCH DAMAGE.
27+
*/
28+
29+
size_t
30+
mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
31+
size_t nms, size_t len, mbstate_t * __restrict ps) {
32+
const char *s;
33+
size_t nchr;
34+
wchar_t wc;
35+
size_t nb;
36+
37+
s = *src;
38+
nchr = 0;
39+
40+
if (dst == NULL) {
41+
for (;;) {
42+
if ((nb = mbrtowc(&wc, s, nms, ps)) == (size_t)-1)
43+
/* Invalid sequence - mbrtowc() sets errno. */
44+
return ((size_t)-1);
45+
else if (nb == 0 || nb == (size_t)-2)
46+
return (nchr);
47+
s += nb;
48+
nms -= nb;
49+
nchr++;
50+
}
51+
/*NOTREACHED*/
52+
}
53+
54+
while (len-- > 0) {
55+
if ((nb = mbrtowc(dst, s, nms, ps)) == (size_t)-1) {
56+
*src = s;
57+
return ((size_t)-1);
58+
} else if (nb == (size_t)-2) {
59+
*src = s + nms;
60+
return (nchr);
61+
} else if (nb == 0) {
62+
*src = NULL;
63+
return (nchr);
64+
}
65+
s += nb;
66+
nms -= nb;
67+
nchr++;
68+
dst++;
69+
}
70+
*src = s;
71+
return (nchr);
72+
}

libcxx/src/support/ibm/wcsnrtombs.inc

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*-
2+
* Copyright (c) 2002-2004 Tim J. Robbins.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23+
* SUCH DAMAGE.
24+
*/
25+
26+
size_t
27+
wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
28+
size_t nwc, size_t len, mbstate_t * __restrict ps) {
29+
mbstate_t mbsbak;
30+
char buf[MB_CUR_MAX];
31+
const wchar_t *s;
32+
size_t nbytes;
33+
size_t nb;
34+
35+
s = *src;
36+
nbytes = 0;
37+
38+
if (dst == NULL) {
39+
while (nwc-- > 0) {
40+
if ((nb = wcrtomb(buf, *s, ps)) == (size_t)-1)
41+
/* Invalid character - wcrtomb() sets errno. */
42+
return ((size_t)-1);
43+
else if (*s == L'\0')
44+
return (nbytes + nb - 1);
45+
s++;
46+
nbytes += nb;
47+
}
48+
return (nbytes);
49+
}
50+
51+
while (len > 0 && nwc-- > 0) {
52+
if (len > (size_t)MB_CUR_MAX) {
53+
/* Enough space to translate in-place. */
54+
if ((nb = wcrtomb(dst, *s, ps)) == (size_t)-1) {
55+
*src = s;
56+
return ((size_t)-1);
57+
}
58+
} else {
59+
/*
60+
* May not be enough space; use temp. buffer.
61+
*
62+
* We need to save a copy of the conversion state
63+
* here so we can restore it if the multibyte
64+
* character is too long for the buffer.
65+
*/
66+
mbsbak = *ps;
67+
if ((nb = wcrtomb(buf, *s, ps)) == (size_t)-1) {
68+
*src = s;
69+
return ((size_t)-1);
70+
}
71+
if (nb > len) {
72+
/* MB sequence for character won't fit. */
73+
*ps = mbsbak;
74+
break;
75+
}
76+
memcpy(dst, buf, nb);
77+
}
78+
if (*s == L'\0') {
79+
*src = NULL;
80+
return (nbytes + nb - 1);
81+
}
82+
s++;
83+
dst += nb;
84+
len -= nb;
85+
nbytes += nb;
86+
}
87+
*src = s;
88+
return (nbytes);
89+
}
90+

0 commit comments

Comments
 (0)