wcwidth.c revision 102124
1102033Sache/* 2102033Sache * Copyright (c) 1989, 1993 3102033Sache * The Regents of the University of California. All rights reserved. 4102033Sache * (c) UNIX System Laboratories, Inc. 5102033Sache * All or some portions of this file are derived from material licensed 6102033Sache * to the University of California by American Telephone and Telegraph 7102033Sache * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8102033Sache * the permission of UNIX System Laboratories, Inc. 9102033Sache * 10102033Sache * This code is derived from software contributed to Berkeley by 11102033Sache * Paul Borman at Krystal Technologies. 12102033Sache * 13102033Sache * Redistribution and use in source and binary forms, with or without 14102033Sache * modification, are permitted provided that the following conditions 15102033Sache * are met: 16102033Sache * 1. Redistributions of source code must retain the above copyright 17102033Sache * notice, this list of conditions and the following disclaimer. 18102033Sache * 2. Redistributions in binary form must reproduce the above copyright 19102033Sache * notice, this list of conditions and the following disclaimer in the 20102033Sache * documentation and/or other materials provided with the distribution. 21102033Sache * 3. All advertising materials mentioning features or use of this software 22102033Sache * must display the following acknowledgement: 23102033Sache * This product includes software developed by the University of 24102033Sache * California, Berkeley and its contributors. 25102033Sache * 4. Neither the name of the University nor the names of its contributors 26102033Sache * may be used to endorse or promote products derived from this software 27102033Sache * without specific prior written permission. 28102033Sache * 29102033Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30102033Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31102033Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32102033Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33102033Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34102033Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35102033Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36102033Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37102033Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38102033Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39102033Sache * SUCH DAMAGE. 40102033Sache */ 41102033Sache 42102033Sache#include <sys/cdefs.h> 43102033Sache__FBSDID("$FreeBSD: head/lib/libc/locale/wcwidth.c 102124 2002-08-19 18:06:18Z ache $"); 44102033Sache 45102033Sache#include <wchar.h> 46102033Sache#include <wctype.h> 47102033Sache 48102096Sache#define _CTYPE_SWM 0xe0000000L /* Mask to get screen width data */ 49102096Sache#define _CTYPE_SWS 30 /* Bits to shift to get width */ 50102096Sache 51102033Sacheint 52102033Sachewcwidth(wc) 53102033Sache wchar_t wc; 54102033Sache{ 55102124Sache int width; 56102033Sache 57102124Sache if (wc == 0) 58102124Sache return (0); 59102124Sache 60102124Sache width = __maskrune(wc, _CTYPE_SWM); 61102124Sache 62102096Sache /* 0 is autowidth (default) */ 63102033Sache return (width ? (int)((unsigned)width >> _CTYPE_SWS) 64102033Sache : (iswprint(wc) ? 1 : -1)); 65102033Sache} 66102033Sache 67