xref: /openbsd/lib/libc/locale/mbtowc.3 (revision 898184e3)
1.\" $OpenBSD: mbtowc.3,v 1.3 2010/11/20 18:23:19 stsp Exp $
2.\" $NetBSD: mbtowc.3,v 1.5 2003/09/08 17:54:31 wiz Exp $
3.\"
4.\" Copyright (c)2002 Citrus Project,
5.\" All rights reserved.
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.Dd $Mdocdate: November 20 2010 $
29.Dt MBTOWC 3
30.Os
31.\" ----------------------------------------------------------------------
32.Sh NAME
33.Nm mbtowc
34.Nd converts a multibyte character to a wide character
35.\" ----------------------------------------------------------------------
36.Sh SYNOPSIS
37.Fd #include <stdlib.h>
38.Ft int
39.Fn mbtowc "wchar_t * restrict pwc" "const char * restrict s" "size_t n"
40.Sh DESCRIPTION
41The
42.Fn mbtowc
43usually converts the multibyte character pointed to by
44.Fa s
45to a wide character, and stores it in the wchar_t object pointed to by
46.Fa pwc
47if
48.Fa pwc
49is non-null and
50.Fa s
51points to a valid character.
52This function may inspect at most n bytes of the array beginning from
53.Fa s .
54.Pp
55In state-dependent encodings,
56.Fa s
57may point to the special sequence bytes to change the shift-state.
58Although such sequence bytes correspond to no individual
59wide-character code,
60.Fn mbtowc
61changes its own state by the sequence bytes and treats them
62as if they are a part of the subsequence multibyte character.
63.Pp
64Unlike
65.Xr mbrtowc 3 ,
66the first
67.Fa n
68bytes pointed to by
69.Fa s
70need to form an entire multibyte character.
71Otherwise, this function causes an error.
72.Pp
73Calling any other functions in
74.Em libc
75never change the internal
76state of the
77.Fn mbtowc ,
78except for calling
79.Xr setlocale 3
80with the
81.Dv LC_CTYPE
82category changed to that of the current locale.
83Such
84.Xr setlocale 3
85calls cause the internal state of this function to be indeterminate.
86.Pp
87The behaviour of
88.Fn mbtowc
89is affected by the
90.Dv LC_CTYPE
91category of the current locale.
92.Pp
93These are the special cases:
94.Bl -tag -width 012345678901
95.It s == NULL
96.Fn mbtowc
97initializes its own internal state to an initial state, and
98determines whether the current encoding is state-dependent.
99This function returns 0 if the encoding is state-independent,
100otherwise non-zero.
101In this case,
102.Fa pwc
103is completely ignored.
104.It pwc == NULL
105.Fn mbtowc
106executes the conversion as if
107.Fa pwc
108is non-null, but a result of the conversion is discarded.
109.It n == 0
110In this case,
111the first
112.Fa n
113bytes of the array pointed to by
114.Fa s
115never form a complete character.
116Thus, the
117.Fn mbtowc
118always fails.
119.El
120.\" ----------------------------------------------------------------------
121.Sh RETURN VALUES
122Normally,
123.Fn mbtowc
124returns:
125.Bl -tag -width 012345678901
126.It 0
127.Fa s
128points to a null byte
129.Pq Sq \e0 .
130.It positive
131Number of bytes for the valid multibyte character pointed to by
132.Fa s .
133There are no cases where the value returned is greater than
134the value of the
135.Dv MB_CUR_MAX
136macro.
137.It -1
138.Fa s
139points to an invalid or an incomplete multibyte character.
140The
141.Fn mbtowc
142also sets errno to indicate the error.
143.El
144.Pp
145When
146.Fa s
147is equal to NULL,
148.Fn mbtowc
149returns:
150.Bl -tag -width 0123456789
151.It 0
152The current encoding is state-independent.
153.It non-zero
154The current encoding is state-dependent.
155.El
156.\" ----------------------------------------------------------------------
157.Sh ERRORS
158.Fn mbtowc
159may cause an error in the following cases:
160.Bl -tag -width Er
161.It Bq Er EILSEQ
162.Fa s
163points to an invalid or incomplete multibyte character.
164.El
165.\" ----------------------------------------------------------------------
166.Sh SEE ALSO
167.Xr mblen 3 ,
168.Xr mbrtowc 3 ,
169.Xr setlocale 3
170.\" ----------------------------------------------------------------------
171.Sh STANDARDS
172The
173.Fn mbtowc
174function conforms to
175.St -ansiC .
176The restrict qualifier is added at
177.\" .St -isoC99 .
178ISO/IEC 9899/1999
179.Pq Dq ISO C99 .
180.Sh CAVEATS
181On error, callers of
182.Fn mbtowc
183cannot tell whether the multibyte character was invalid or incomplete.
184To treat incomplete data differently from invalid data the
185.Xr mbrtowc 3
186function can be used instead.
187