wcwidth.c revision 102033
1118722Stjr/* 2118722Stjr * Copyright (c) 1989, 1993 3118722Stjr * The Regents of the University of California. All rights reserved. 4118722Stjr * (c) UNIX System Laboratories, Inc. 5118722Stjr * All or some portions of this file are derived from material licensed 6118722Stjr * to the University of California by American Telephone and Telegraph 7118722Stjr * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8118722Stjr * the permission of UNIX System Laboratories, Inc. 9118722Stjr * 10118722Stjr * This code is derived from software contributed to Berkeley by 11118722Stjr * Paul Borman at Krystal Technologies. 12118722Stjr * 13118722Stjr * Redistribution and use in source and binary forms, with or without 14118722Stjr * modification, are permitted provided that the following conditions 15118722Stjr * are met: 16118722Stjr * 1. Redistributions of source code must retain the above copyright 17118722Stjr * notice, this list of conditions and the following disclaimer. 18118722Stjr * 2. Redistributions in binary form must reproduce the above copyright 19118722Stjr * notice, this list of conditions and the following disclaimer in the 20118722Stjr * documentation and/or other materials provided with the distribution. 21118722Stjr * 3. All advertising materials mentioning features or use of this software 22118722Stjr * must display the following acknowledgement: 23118722Stjr * This product includes software developed by the University of 24118722Stjr * California, Berkeley and its contributors. 25118722Stjr * 4. Neither the name of the University nor the names of its contributors 26131608Sru * may be used to endorse or promote products derived from this software 27118722Stjr * without specific prior written permission. 28118722Stjr * 29118722Stjr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30118722Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31118722Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32118722Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33118722Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34118722Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35118722Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36118722Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37118722Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38118722Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39118722Stjr * SUCH DAMAGE. 40118722Stjr */ 41118722Stjr 42118722Stjr#include <sys/cdefs.h> 43131608Sru__FBSDID("$FreeBSD: head/lib/libc/locale/wcwidth.c 102033 2002-08-17 20:30:34Z ache $"); 44118722Stjr 45118722Stjr#include <wchar.h> 46118722Stjr#include <wctype.h> 47118722Stjr 48122730Stjrint 49118722Stjrwcwidth(wc) 50122287Stjr wchar_t wc; 51118722Stjr{ 52118722Stjr int width = __maskrune(wc, _CTYPE_SWM); 53118722Stjr 54118722Stjr return (width ? (int)((unsigned)width >> _CTYPE_SWS) 55118722Stjr : (iswprint(wc) ? 1 : -1)); 56118722Stjr} 57118722Stjr 58118722Stjr