mblen.c revision 227753
1102697Stjr/*- 2127944Stjr * Copyright (c) 2002-2004 Tim J. Robbins. 3118595Stjr * All rights reserved. 4102697Stjr * 5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation 6227753Stheraven * All rights reserved. 7227753Stheraven * Portions of this software were developed by David Chisnall 8227753Stheraven * under sponsorship from the FreeBSD Foundation. 9227753Stheraven * 10102697Stjr * Redistribution and use in source and binary forms, with or without 11102697Stjr * modification, are permitted provided that the following conditions 12102697Stjr * are met: 13102697Stjr * 1. Redistributions of source code must retain the above copyright 14102697Stjr * notice, this list of conditions and the following disclaimer. 15102697Stjr * 2. Redistributions in binary form must reproduce the above copyright 16102697Stjr * notice, this list of conditions and the following disclaimer in the 17102697Stjr * documentation and/or other materials provided with the distribution. 18102697Stjr * 19118595Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20102697Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21102697Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22118595Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23102697Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24102697Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25102697Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26102697Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27102697Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28102697Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29102697Stjr * SUCH DAMAGE. 30102697Stjr */ 31102697Stjr 32102697Stjr#include <sys/cdefs.h> 33102697Stjr__FBSDID("$FreeBSD: head/lib/libc/locale/mblen.c 227753 2011-11-20 14:45:42Z theraven $"); 34102697Stjr 35102697Stjr#include <stdlib.h> 36127944Stjr#include <wchar.h> 37129154Stjr#include "mblocal.h" 38102697Stjr 39102697Stjrint 40227753Stheravenmblen_l(const char *s, size_t n, locale_t locale) 41102697Stjr{ 42127944Stjr static const mbstate_t initial; 43127944Stjr size_t rval; 44227753Stheraven FIX_LOCALE(locale); 45102697Stjr 46127944Stjr if (s == NULL) { 47127944Stjr /* No support for state dependent encodings. */ 48227753Stheraven locale->mblen = initial; 49127944Stjr return (0); 50127944Stjr } 51227753Stheraven rval = XLOCALE_CTYPE(locale)->__mbrtowc(NULL, s, n, &locale->mblen); 52127944Stjr if (rval == (size_t)-1 || rval == (size_t)-2) 53127944Stjr return (-1); 54127944Stjr return ((int)rval); 55102697Stjr} 56227753Stheraven 57227753Stheravenint 58227753Stheravenmblen(const char *s, size_t n) 59227753Stheraven{ 60227753Stheraven return mblen_l(s, n, __get_locale()); 61227753Stheraven} 62