1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
.\" $OpenBSD: wcrtomb.3,v 1.12 2023/09/12 08:33:37 jsg Exp $
.\" $NetBSD: wcrtomb.3,v 1.4 2003/09/08 17:54:31 wiz Exp $
.\"
.\" Copyright (c)2023 Ingo Schwarze <schwarze@openbsd.org>
.\" Copyright (c)2002 Citrus Project,
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd $Mdocdate: September 12 2023 $
.Dt WCRTOMB 3
.Os
.Sh NAME
.Nm wcrtomb ,
.Nm c32rtomb
.Nd convert a wide character to a multibyte character
.Sh SYNOPSIS
.In wchar.h
.Ft size_t
.Fo wcrtomb
.Fa "const char * restrict s"
.Fa "wchar_t wc"
.Fa "mbstate_t * restrict mbs"
.Fc
.In uchar.h
.Ft size_t
.Fo c32rtomb
.Fa "char * restrict s"
.Fa "char32_t wc"
.Fa "mbstate_t * restrict mbs"
.Fc
.Sh DESCRIPTION
.Fn wcrtomb
and
.Fn c32rtomb
convert the wide character
.Fa wc
to the corresponding multibyte character, and store up to
.Dv MB_CUR_MAX
bytes in the array pointed to by
.Fa s
if
.Fa s
is not a
.Dv NULL
pointer.
The interpretation of
.Fa wc
is implementation-defined.
On
.Ox ,
.Vt wchar_t
and
.Vt char32_t
are of the same width and both are always interpreted as Unicode codepoints.
.Pp
The output encoding that
.Fn wcrtomb
and
.Fn c32rtomb
use in
.Fa s
is determined by the
.Dv LC_CTYPE
category of the current locale.
.Ox
only supports UTF-8 and ASCII output,
and these functions are only useful for UTF-8.
.Pp
The following arguments cause special processing:
.Bl -tag -width 012345678901
.It Fa wc No == 0
A NUL byte is stored to
.Pf * Fa s
and the state object pointed to by
.Fa mbs
is reset to the initial state.
On operating systems other than
.Ox
that support state-dependent multibyte encodings, a special byte sequence
.Pq Dq shift sequence
is written before the NUL byte to return to the initial state
if that is required by the output encoding
and by the current output encoding state.
.It Fa mbs No == Dv NULL
.Fn mbrtowc
and
.Fn c32rtomb
each use their own internal state object instead of the
.Fa mbs
argument.
Both internal state objects are initialized at startup time of the program,
and no other
.Em libc
function ever changes either of them.
.It Fa s No == Dv NULL
The object pointed to by
.Fa mbs ,
or the internal object if
.Fa mbs
is a
.Dv NULL
pointer, is reset to the initial state,
.Fa wc
is ignored, and 1 is returned.
.El
.Sh RETURN VALUES
.Fn wcrtomb
and
.Fn c32rtomb
return the number of bytes (including any shift sequences)
which are stored in the array pointed to by
.Fa s ,
or 1 if
.Fa s
is
.Dv NULL .
If
.Fa wc
is not a valid wide character
or if it cannot be represented in the multibyte encoding selected with
.Dv LC_CTYPE ,
both functions return
.Po Vt size_t Pc Ns \-1
and set
.Va errno
to indicate the error.
.Sh ERRORS
.Fn wcrtomb
and
.Fn c32rtomb
cause an error in the following cases:
.Bl -tag -width Er
.It Bq Er EILSEQ
.Fa wc
is not a valid wide character or cannot be represented using
.Dv LC_CTYPE .
.It Bq Er EINVAL
.Fa mbs
points to an invalid or uninitialized
.Vt mbstate_t
object.
.El
.Sh SEE ALSO
.Xr mbrtowc 3 ,
.Xr setlocale 3 ,
.Xr wctomb 3
.Sh STANDARDS
.Fn wcrtomb
conforms to
.St -isoC-amd1 .
The restrict qualifier was added at
.St -isoC-99 .
.Pp
.Fn c32rtomb
conforms to
.St -isoC-2011 .
.Sh HISTORY
.Fn wcrtomb
has been available since
.Ox 3.8
and has provided support for UTF-8 since
.Ox 4.8 .
.Pp
.Fn c32rtomb
has been available since
.Ox 7.4 .
|