xref: /dflybsd-src/lib/libc/locale/mblen.c (revision 0d5acd7467c4e95f792ef49fceb3ab8e917ce86b)
1*0d5acd74SJohn Marino /*-
2*0d5acd74SJohn Marino  * Copyright (c) 2002-2004 Tim J. Robbins.
3*0d5acd74SJohn Marino  * All rights reserved.
4*0d5acd74SJohn Marino  *
5*0d5acd74SJohn Marino  * Copyright (c) 2011 The FreeBSD Foundation
6*0d5acd74SJohn Marino  * All rights reserved.
7*0d5acd74SJohn Marino  * Portions of this software were developed by David Chisnall
8*0d5acd74SJohn Marino  * under sponsorship from the FreeBSD Foundation.
9*0d5acd74SJohn Marino  *
10*0d5acd74SJohn Marino  * Redistribution and use in source and binary forms, with or without
11*0d5acd74SJohn Marino  * modification, are permitted provided that the following conditions
12*0d5acd74SJohn Marino  * are met:
13*0d5acd74SJohn Marino  * 1. Redistributions of source code must retain the above copyright
14*0d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer.
15*0d5acd74SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
16*0d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
17*0d5acd74SJohn Marino  *    documentation and/or other materials provided with the distribution.
18*0d5acd74SJohn Marino  *
19*0d5acd74SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20*0d5acd74SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*0d5acd74SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*0d5acd74SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23*0d5acd74SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*0d5acd74SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*0d5acd74SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*0d5acd74SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*0d5acd74SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*0d5acd74SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0d5acd74SJohn Marino  * SUCH DAMAGE.
30*0d5acd74SJohn Marino  *
31*0d5acd74SJohn Marino  * $FreeBSD: head/lib/libc/locale/mblen.c 227753 2011-11-20 14:45:42Z theraven $
32*0d5acd74SJohn Marino  */
33*0d5acd74SJohn Marino 
34*0d5acd74SJohn Marino 
35*0d5acd74SJohn Marino #include <stdlib.h>
36*0d5acd74SJohn Marino #include <wchar.h>
37*0d5acd74SJohn Marino #include "mblocal.h"
38*0d5acd74SJohn Marino 
39*0d5acd74SJohn Marino int
mblen_l(const char * s,size_t n,locale_t locale)40*0d5acd74SJohn Marino mblen_l(const char *s, size_t n, locale_t locale)
41*0d5acd74SJohn Marino {
42*0d5acd74SJohn Marino 	static const mbstate_t initial;
43*0d5acd74SJohn Marino 	size_t rval;
44*0d5acd74SJohn Marino 	FIX_LOCALE(locale);
45*0d5acd74SJohn Marino 
46*0d5acd74SJohn Marino 	if (s == NULL) {
47*0d5acd74SJohn Marino 		/* No support for state dependent encodings. */
48*0d5acd74SJohn Marino 		locale->mblen = initial;
49*0d5acd74SJohn Marino 		return (0);
50*0d5acd74SJohn Marino 	}
51*0d5acd74SJohn Marino 	rval = XLOCALE_CTYPE(locale)->__mbrtowc(NULL, s, n, &locale->mblen);
52*0d5acd74SJohn Marino 	if (rval == (size_t)-1 || rval == (size_t)-2)
53*0d5acd74SJohn Marino 		return (-1);
54*0d5acd74SJohn Marino 	return ((int)rval);
55*0d5acd74SJohn Marino }
56*0d5acd74SJohn Marino 
57*0d5acd74SJohn Marino int
mblen(const char * s,size_t n)58*0d5acd74SJohn Marino mblen(const char *s, size_t n)
59*0d5acd74SJohn Marino {
60*0d5acd74SJohn Marino 	return mblen_l(s, n, __get_locale());
61*0d5acd74SJohn Marino }
62