xref: /freebsd/lib/libc/locale/mblen.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
17438fc3aSTim J. Robbins /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
474f90defSTim J. Robbins  * Copyright (c) 2002-2004 Tim J. Robbins.
5b85aa4e3STim J. Robbins  * All rights reserved.
67438fc3aSTim J. Robbins  *
73c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
85b5fa75aSEd Maste  *
93c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
103c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
113c87aa1dSDavid Chisnall  *
127438fc3aSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
137438fc3aSTim J. Robbins  * modification, are permitted provided that the following conditions
147438fc3aSTim J. Robbins  * are met:
157438fc3aSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
167438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
177438fc3aSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
187438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
197438fc3aSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
207438fc3aSTim J. Robbins  *
21b85aa4e3STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
227438fc3aSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237438fc3aSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b85aa4e3STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
257438fc3aSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267438fc3aSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277438fc3aSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287438fc3aSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297438fc3aSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307438fc3aSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317438fc3aSTim J. Robbins  * SUCH DAMAGE.
327438fc3aSTim J. Robbins  */
337438fc3aSTim J. Robbins 
34b6f33850STim J. Robbins #include <stdlib.h>
3574f90defSTim J. Robbins #include <wchar.h>
366155c34aSTim J. Robbins #include "mblocal.h"
377438fc3aSTim J. Robbins 
387438fc3aSTim J. Robbins int
mblen_l(const char * s,size_t n,locale_t locale)393c87aa1dSDavid Chisnall mblen_l(const char *s, size_t n, locale_t locale)
407438fc3aSTim J. Robbins {
4174f90defSTim J. Robbins 	static const mbstate_t initial;
4274f90defSTim J. Robbins 	size_t rval;
433c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
447438fc3aSTim J. Robbins 
4574f90defSTim J. Robbins 	if (s == NULL) {
4674f90defSTim J. Robbins 		/* No support for state dependent encodings. */
479eb7d595SYuri Pankov 		XLOCALE_CTYPE(locale)->mblen = initial;
4874f90defSTim J. Robbins 		return (0);
4974f90defSTim J. Robbins 	}
509eb7d595SYuri Pankov 	rval = XLOCALE_CTYPE(locale)->__mbrtowc(NULL, s, n,
519eb7d595SYuri Pankov 	    &(XLOCALE_CTYPE(locale)->mblen));
5274f90defSTim J. Robbins 	if (rval == (size_t)-1 || rval == (size_t)-2)
5374f90defSTim J. Robbins 		return (-1);
5474f90defSTim J. Robbins 	return ((int)rval);
557438fc3aSTim J. Robbins }
563c87aa1dSDavid Chisnall 
573c87aa1dSDavid Chisnall int
mblen(const char * s,size_t n)583c87aa1dSDavid Chisnall mblen(const char *s, size_t n)
593c87aa1dSDavid Chisnall {
603c87aa1dSDavid Chisnall 	return mblen_l(s, n, __get_locale());
613c87aa1dSDavid Chisnall }
62