wcswidth.c revision 102159
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 * 1376612Stshiozak * Redistribution and use in source and binary forms, with or without 1476612Stshiozak * modification, are permitted provided that the following conditions 1576612Stshiozak * are met: 1676612Stshiozak * 1. Redistributions of source code must retain the above copyright 1776612Stshiozak * notice, this list of conditions and the following disclaimer. 1876612Stshiozak * 2. Redistributions in binary form must reproduce the above copyright 1976612Stshiozak * notice, this list of conditions and the following disclaimer in the 2076612Stshiozak * documentation and/or other materials provided with the distribution. 21102159Sache * 3. All advertising materials mentioning features or use of this software 22102159Sache * must display the following acknowledgement: 23102159Sache * This product includes software developed by the University of 24102159Sache * California, Berkeley and its contributors. 25102159Sache * 4. Neither the name of the University nor the names of its contributors 26102159Sache * may be used to endorse or promote products derived from this software 27102159Sache * without specific prior written permission. 2876612Stshiozak * 29102159Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 3076612Stshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3176612Stshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32102159Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3376612Stshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3476612Stshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3576612Stshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3676612Stshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3776612Stshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3876612Stshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3976612Stshiozak * SUCH DAMAGE. 4076612Stshiozak */ 4176612Stshiozak 4276612Stshiozak#include <sys/cdefs.h> 4386170Sobrien__FBSDID("$FreeBSD: head/lib/libc/string/wcswidth.c 102159 2002-08-20 02:06:28Z ache $"); 4476612Stshiozak 4576612Stshiozak#include <wchar.h> 4676612Stshiozak 4776612Stshiozakint 48102159Sachewcswidth(const wchar_t *pwcs, size_t n) 4976612Stshiozak{ 50102159Sache wchar_t wc; 51102159Sache int len, l; 5276612Stshiozak 53102159Sache len = 0; 54102159Sache while (n-- > 0 && (wc = *pwcs++) != L'\0') { 55102159Sache if ((l = wcwidth(wc)) < 0) 56102159Sache return (-1); 57102159Sache len += l; 5876612Stshiozak } 59102159Sache return (len); 60102159Sache} 6176612Stshiozak 62