18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4353450fbSAndrey A. Chernov * Copyright (c) 1989, 1993
5353450fbSAndrey A. Chernov * The Regents of the University of California. All rights reserved.
6353450fbSAndrey A. Chernov * (c) UNIX System Laboratories, Inc.
7353450fbSAndrey A. Chernov * All or some portions of this file are derived from material licensed
8353450fbSAndrey A. Chernov * to the University of California by American Telephone and Telegraph
9353450fbSAndrey A. Chernov * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10353450fbSAndrey A. Chernov * the permission of UNIX System Laboratories, Inc.
11353450fbSAndrey A. Chernov *
12353450fbSAndrey A. Chernov * This code is derived from software contributed to Berkeley by
13353450fbSAndrey A. Chernov * Paul Borman at Krystal Technologies.
149829d36aSTakuya SHIOZAKI *
153c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation
16*5b5fa75aSEd Maste *
173c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall
183c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation.
193c87aa1dSDavid Chisnall *
209829d36aSTakuya SHIOZAKI * Redistribution and use in source and binary forms, with or without
219829d36aSTakuya SHIOZAKI * modification, are permitted provided that the following conditions
229829d36aSTakuya SHIOZAKI * are met:
239829d36aSTakuya SHIOZAKI * 1. Redistributions of source code must retain the above copyright
249829d36aSTakuya SHIOZAKI * notice, this list of conditions and the following disclaimer.
259829d36aSTakuya SHIOZAKI * 2. Redistributions in binary form must reproduce the above copyright
269829d36aSTakuya SHIOZAKI * notice, this list of conditions and the following disclaimer in the
279829d36aSTakuya SHIOZAKI * documentation and/or other materials provided with the distribution.
283fb3b97cSEd Maste * 3. Neither the name of the University nor the names of its contributors
29353450fbSAndrey A. Chernov * may be used to endorse or promote products derived from this software
30353450fbSAndrey A. Chernov * without specific prior written permission.
319829d36aSTakuya SHIOZAKI *
32353450fbSAndrey A. Chernov * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
339829d36aSTakuya SHIOZAKI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
349829d36aSTakuya SHIOZAKI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35353450fbSAndrey A. Chernov * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
369829d36aSTakuya SHIOZAKI * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
379829d36aSTakuya SHIOZAKI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
389829d36aSTakuya SHIOZAKI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
399829d36aSTakuya SHIOZAKI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
409829d36aSTakuya SHIOZAKI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
419829d36aSTakuya SHIOZAKI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
429829d36aSTakuya SHIOZAKI * SUCH DAMAGE.
439829d36aSTakuya SHIOZAKI */
449829d36aSTakuya SHIOZAKI
459829d36aSTakuya SHIOZAKI #include <wchar.h>
463c87aa1dSDavid Chisnall #include "xlocale_private.h"
479829d36aSTakuya SHIOZAKI
489829d36aSTakuya SHIOZAKI int
wcswidth_l(const wchar_t * pwcs,size_t n,locale_t locale)493c87aa1dSDavid Chisnall wcswidth_l(const wchar_t *pwcs, size_t n, locale_t locale)
509829d36aSTakuya SHIOZAKI {
51353450fbSAndrey A. Chernov wchar_t wc;
52353450fbSAndrey A. Chernov int len, l;
533c87aa1dSDavid Chisnall FIX_LOCALE(locale);
549829d36aSTakuya SHIOZAKI
55353450fbSAndrey A. Chernov len = 0;
56353450fbSAndrey A. Chernov while (n-- > 0 && (wc = *pwcs++) != L'\0') {
573c87aa1dSDavid Chisnall if ((l = wcwidth_l(wc, locale)) < 0)
58353450fbSAndrey A. Chernov return (-1);
59353450fbSAndrey A. Chernov len += l;
60353450fbSAndrey A. Chernov }
61353450fbSAndrey A. Chernov return (len);
629829d36aSTakuya SHIOZAKI }
639829d36aSTakuya SHIOZAKI
643c87aa1dSDavid Chisnall int
wcswidth(const wchar_t * pwcs,size_t n)653c87aa1dSDavid Chisnall wcswidth(const wchar_t *pwcs, size_t n)
663c87aa1dSDavid Chisnall {
673c87aa1dSDavid Chisnall return wcswidth_l(pwcs, n, __get_locale());
683c87aa1dSDavid Chisnall }
69