mbsrtowcs.c revision 102050
1102050Stjr/*- 2102050Stjr * Copyright (c) 2002 Tim J. Robbins. 3102050Stjr * All rights reserved. 4102050Stjr * 5102050Stjr * Redistribution and use in source and binary forms, with or without 6102050Stjr * modification, are permitted provided that the following conditions 7102050Stjr * are met: 8102050Stjr * 1. Redistributions of source code must retain the above copyright 9102050Stjr * notice, this list of conditions and the following disclaimer. 10102050Stjr * 2. Redistributions in binary form must reproduce the above copyright 11102050Stjr * notice, this list of conditions and the following disclaimer in the 12102050Stjr * documentation and/or other materials provided with the distribution. 13102050Stjr * 14102050Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15102050Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16102050Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17102050Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18102050Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19102050Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20102050Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21102050Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22102050Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23102050Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24102050Stjr * SUCH DAMAGE. 25102050Stjr */ 26102050Stjr 27102050Stjr#include <sys/cdefs.h> 28102050Stjr__FBSDID("$FreeBSD: head/lib/libc/locale/mbsrtowcs.c 102050 2002-08-18 06:30:10Z tjr $"); 29102050Stjr 30102050Stjr#include <errno.h> 31102050Stjr#include <limits.h> 32102050Stjr#include <stdlib.h> 33102050Stjr#include <wchar.h> 34102050Stjr 35102050Stjrsize_t 36102050Stjrmbsrtowcs(wchar_t *__restrict dst, const char **__restrict src, size_t len, 37102050Stjr mbstate_t *__restrict ps __unused) 38102050Stjr{ 39102050Stjr const char *s; 40102050Stjr size_t nchr; 41102050Stjr wchar_t wc; 42102050Stjr int nb; 43102050Stjr 44102050Stjr s = *src; 45102050Stjr nchr = 0; 46102050Stjr 47102050Stjr if (dst == NULL) { 48102050Stjr for (;;) { 49102050Stjr if ((nb = (int)mbrtowc(&wc, s, MB_CUR_MAX, NULL)) < 0) 50102050Stjr /* Invalid sequence - mbrtowc() sets errno. */ 51102050Stjr return ((size_t)-1); 52102050Stjr else if (nb == 0) 53102050Stjr return (nchr); 54102050Stjr s += nb; 55102050Stjr nchr++; 56102050Stjr } 57102050Stjr /*NOTREACHED*/ 58102050Stjr } 59102050Stjr 60102050Stjr while (len-- > 0) { 61102050Stjr if ((nb = (int)mbrtowc(dst, s, MB_CUR_MAX, NULL)) < 0) { 62102050Stjr *src = s; 63102050Stjr return ((size_t)-1); 64102050Stjr } else if (nb == 0) { 65102050Stjr *src = NULL; 66102050Stjr return (nchr); 67102050Stjr } 68102050Stjr s += nb; 69102050Stjr nchr++; 70102050Stjr dst++; 71102050Stjr } 72102050Stjr *src = s; 73102050Stjr return (nchr); 74102050Stjr} 75