1102697Stjr/*- 2127944Stjr * Copyright (c) 2002-2004 Tim J. Robbins. 3118591Stjr * 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 * 19118591Stjr * 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 22118591Stjr * 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$"); 34102697Stjr 35297440Spfg#include <errno.h> 36102697Stjr#include <stdlib.h> 37118591Stjr#include <wchar.h> 38129154Stjr#include "mblocal.h" 39102697Stjr 40102697Stjrint 41227753Stheravenmbtowc_l(wchar_t * __restrict pwc, const char * __restrict s, size_t n, locale_t locale) 42102697Stjr{ 43127944Stjr static const mbstate_t initial; 44118591Stjr size_t rval; 45227753Stheraven FIX_LOCALE(locale); 46102697Stjr 47127944Stjr if (s == NULL) { 48106032Stjr /* No support for state dependent encodings. */ 49227753Stheraven locale->mbtowc = initial; 50118591Stjr return (0); 51127944Stjr } 52227753Stheraven rval = XLOCALE_CTYPE(locale)->__mbrtowc(pwc, s, n, &locale->mbtowc); 53297440Spfg switch (rval) { 54297440Spfg case (size_t)-2: 55297440Spfg errno = EILSEQ; 56297440Spfg /* FALLTHROUGH */ 57297440Spfg case (size_t)-1: 58106077Stjr return (-1); 59297440Spfg default: 60297440Spfg return ((int)rval); 61297440Spfg } 62102697Stjr} 63227753Stheravenint 64227753Stheravenmbtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n) 65227753Stheraven{ 66227753Stheraven return mbtowc_l(pwc, s, n, __get_locale()); 67227753Stheraven} 68