xref: /freebsd/lib/libc/locale/mblen.c (revision 74f90def09fb8e53503cdf5d6548619653ed3580)
17438fc3aSTim J. Robbins /*-
274f90defSTim J. Robbins  * Copyright (c) 2002-2004 Tim J. Robbins.
3b85aa4e3STim J. Robbins  * All rights reserved.
47438fc3aSTim J. Robbins  *
57438fc3aSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
67438fc3aSTim J. Robbins  * modification, are permitted provided that the following conditions
77438fc3aSTim J. Robbins  * are met:
87438fc3aSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
97438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
107438fc3aSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
117438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
127438fc3aSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
137438fc3aSTim J. Robbins  *
14b85aa4e3STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157438fc3aSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167438fc3aSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17b85aa4e3STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187438fc3aSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197438fc3aSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207438fc3aSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217438fc3aSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227438fc3aSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237438fc3aSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247438fc3aSTim J. Robbins  * SUCH DAMAGE.
257438fc3aSTim J. Robbins  */
267438fc3aSTim J. Robbins 
277438fc3aSTim J. Robbins #include <sys/cdefs.h>
287438fc3aSTim J. Robbins __FBSDID("$FreeBSD$");
297438fc3aSTim J. Robbins 
3074f90defSTim J. Robbins #include <errno.h>
3174f90defSTim J. Robbins #include <limits.h>
32b6f33850STim J. Robbins #include <stdlib.h>
3374f90defSTim J. Robbins #include <wchar.h>
347438fc3aSTim J. Robbins 
357438fc3aSTim J. Robbins int
36b6f33850STim J. Robbins mblen(const char *s, size_t n)
377438fc3aSTim J. Robbins {
3874f90defSTim J. Robbins 	static const mbstate_t initial;
3974f90defSTim J. Robbins 	static mbstate_t mbs;
4074f90defSTim J. Robbins 	size_t rval;
417438fc3aSTim J. Robbins 
4274f90defSTim J. Robbins 	if (s == NULL) {
4374f90defSTim J. Robbins 		/* No support for state dependent encodings. */
4474f90defSTim J. Robbins 		mbs = initial;
4574f90defSTim J. Robbins 		return (0);
4674f90defSTim J. Robbins 	}
4774f90defSTim J. Robbins 	rval = mbrtowc(NULL, s, n, &mbs);
4874f90defSTim J. Robbins 	if (rval == (size_t)-1 || rval == (size_t)-2)
4974f90defSTim J. Robbins 		return (-1);
5074f90defSTim J. Robbins 	if (rval > INT_MAX) {
5174f90defSTim J. Robbins 		errno = ERANGE;
5274f90defSTim J. Robbins 		return (-1);
5374f90defSTim J. Robbins 	}
5474f90defSTim J. Robbins 	return ((int)rval);
557438fc3aSTim J. Robbins }
56