1.\" Copyright (c) 2002-2004 Tim J. Robbins 2.\" All rights reserved. 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.\" $FreeBSD: head/lib/libc/locale/mbrtowc.3 250883 2013-05-21 19:59:37Z ed $ 26.\" 27.Dd December 26, 2013 28.Dt MBRTOWC 3 29.Os 30.Sh NAME 31.Nm mbrtowc , 32.Nm mbrtowc_l , 33.Nm mbrtoc16 , 34.Nm mbrtoc32 35.Nd "convert a character to a wide-character code (restartable)" 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In wchar.h 40.Ft size_t 41.Fo mbrtowc 42.Fa "wchar_t * restrict pc" "const char * restrict s" "size_t n" 43.Fa "mbstate_t * restrict ps" 44.Fc 45.In uchar.h 46.Ft size_t 47.Fo mbrtoc16 48.Fa "char16_t * restrict pc" "const char * restrict s" "size_t n" 49.Fa "mbstate_t * restrict ps" 50.Fc 51.Ft size_t 52.Fo mbrtoc32 53.Fa "char32_t * restrict pc" "const char * restrict s" "size_t n" 54.Fa "mbstate_t * restrict ps" 55.Fc 56.In xlocale.h 57.Ft size_t 58.Fo mbrtowc_l 59.Fa "wchar_t * restrict pc" "const char * restrict s" "size_t n" 60.Fa "mbstate_t * restrict ps" "locale_t locale" 61.Fc 62.Sh DESCRIPTION 63The 64.Fn mbrtowc , 65.Fn mbrtowc_l , 66.Fn mbrtoc16 67and 68.Fn mbrtoc32 69functions inspect at most 70.Fa n 71bytes pointed to by 72.Fa s 73to determine the number of bytes needed to complete the next multibyte 74character. 75If a character can be completed, and 76.Fa pc 77is not 78.Dv NULL , 79the wide character which is represented by 80.Fa s 81is stored in the 82.Vt wchar_t , 83.Vt char16_t 84or 85.Vt char32_t 86it points to. 87.Pp 88If 89.Fa s 90is 91.Dv NULL , 92these functions behave as if 93.Fa pc 94was 95.Dv NULL , 96.Fa s 97was an empty string 98.Pq Qq 99and 100.Fa n 101was 1. 102.Pp 103The 104.Vt mbstate_t 105argument, 106.Fa ps , 107is used to keep track of the shift state. 108If it is 109.Dv NULL , 110these functions use an internal, static 111.Vt mbstate_t 112object, which is initialized to the initial conversion state 113at program startup. 114.Pp 115As a single 116.Vt char16_t 117is not large enough to represent certain multibyte characters, the 118.Fn mbrtoc16 119function may need to be invoked multiple times to convert a single 120multibyte character sequence. 121.Pp 122The 123.Fn mbrtowc_l 124function takes an explicit 125.Fa locale 126argument, whereas the 127.Fn mbrtowc 128function uses the current global or per-thread locale. 129.Sh RETURN VALUES 130The 131.Fn mbrtowc , 132.Fn mbrtowc_l , 133.Fn mbrtoc16 134and 135.Fn mbrtoc32 136functions return: 137.Bl -tag -width indent 138.It 0 139The next 140.Fa n 141or fewer bytes 142represent the null wide character 143.Pq Li "L'\e0'" . 144.It >0 145The next 146.Fa n 147or fewer bytes represent a valid character, these functions 148return the number of bytes used to complete the multibyte character. 149.It Po Vt size_t Pc Ns \-1 150An encoding error has occurred. 151The next 152.Fa n 153or fewer bytes do not contribute to a valid multibyte character. 154.It Po Vt size_t Pc Ns \-2 155The next 156.Fa n 157contribute to, but do not complete, a valid multibyte character sequence, 158and all 159.Fa n 160bytes have been processed. 161.El 162.Pp 163The 164.Fn mbrtoc16 165function also returns: 166.Bl -tag -width indent 167.It Po Vt size_t Pc Ns \-3 168The next character resulting from a previous call has been stored. 169No bytes from the input have been consumed. 170.El 171.Sh ERRORS 172The 173.Fn mbrtowc , 174.Fn mbrtowc_l , 175.Fn mbrtoc16 176and 177.Fn mbrtoc32 178functions will fail if: 179.Bl -tag -width Er 180.It Bq Er EILSEQ 181An invalid multibyte sequence was detected. 182.It Bq Er EINVAL 183The conversion state is invalid. 184.El 185.Sh SEE ALSO 186.Xr mbtowc 3 , 187.Xr multibyte 3 , 188.Xr setlocale 3 , 189.Xr wcrtomb 3 , 190.Xr xlocale 3 191.Sh STANDARDS 192The 193.Fn mbrtowc , 194.Fn mbrtoc16 195and 196.Fn mbrtoc32 197functions conform to 198.St -isoC-2011 . 199