1102159Sache/*
2102159Sache * Copyright (c) 1989, 1993
3102159Sache *	The Regents of the University of California.  All rights reserved.
4102159Sache * (c) UNIX System Laboratories, Inc.
5102159Sache * All or some portions of this file are derived from material licensed
6102159Sache * to the University of California by American Telephone and Telegraph
7102159Sache * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8102159Sache * the permission of UNIX System Laboratories, Inc.
976612Stshiozak *
10102159Sache * This code is derived from software contributed to Berkeley by
11102159Sache * Paul Borman at Krystal Technologies.
12102159Sache *
13227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
14227753Stheraven * All rights reserved.
15227753Stheraven * Portions of this software were developed by David Chisnall
16227753Stheraven * under sponsorship from the FreeBSD Foundation.
17227753Stheraven *
1876612Stshiozak * Redistribution and use in source and binary forms, with or without
1976612Stshiozak * modification, are permitted provided that the following conditions
2076612Stshiozak * are met:
2176612Stshiozak * 1. Redistributions of source code must retain the above copyright
2276612Stshiozak *    notice, this list of conditions and the following disclaimer.
2376612Stshiozak * 2. Redistributions in binary form must reproduce the above copyright
2476612Stshiozak *    notice, this list of conditions and the following disclaimer in the
2576612Stshiozak *    documentation and/or other materials provided with the distribution.
26251069Semaste * 3. Neither the name of the University nor the names of its contributors
27102159Sache *    may be used to endorse or promote products derived from this software
28102159Sache *    without specific prior written permission.
2976612Stshiozak *
30102159Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3176612Stshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3276612Stshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33102159Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3476612Stshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3576612Stshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3676612Stshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3776612Stshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3876612Stshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3976612Stshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4076612Stshiozak * SUCH DAMAGE.
4176612Stshiozak */
4276612Stshiozak
4376612Stshiozak#include <sys/cdefs.h>
4486170Sobrien__FBSDID("$FreeBSD$");
4576612Stshiozak
4676612Stshiozak#include <wchar.h>
47227753Stheraven#include "xlocale_private.h"
4876612Stshiozak
4976612Stshiozakint
50227753Stheravenwcswidth_l(const wchar_t *pwcs, size_t n, locale_t locale)
5176612Stshiozak{
52102159Sache	wchar_t wc;
53102159Sache	int len, l;
54227753Stheraven	FIX_LOCALE(locale);
5576612Stshiozak
56102159Sache	len = 0;
57102159Sache	while (n-- > 0 && (wc = *pwcs++) != L'\0') {
58227753Stheraven		if ((l = wcwidth_l(wc, locale)) < 0)
59102159Sache			return (-1);
60102159Sache		len += l;
6176612Stshiozak	}
62102159Sache	return (len);
63102159Sache}
6476612Stshiozak
65227753Stheravenint
66227753Stheravenwcswidth(const wchar_t *pwcs, size_t n)
67227753Stheraven{
68227753Stheraven	return wcswidth_l(pwcs, n, __get_locale());
69227753Stheraven}
70