14297a3b0SGarrett D'Amore /* 2*2d08521bSGarrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org> 36b5e5868SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 44297a3b0SGarrett D'Amore * Copyright (c) 1989, 1993 54297a3b0SGarrett D'Amore * The Regents of the University of California. All rights reserved. 64297a3b0SGarrett D'Amore * (c) UNIX System Laboratories, Inc. 74297a3b0SGarrett D'Amore * All or some portions of this file are derived from material licensed 84297a3b0SGarrett D'Amore * to the University of California by American Telephone and Telegraph 94297a3b0SGarrett D'Amore * Co. or Unix System Laboratories, Inc. and are reproduced herein with 104297a3b0SGarrett D'Amore * the permission of UNIX System Laboratories, Inc. 114297a3b0SGarrett D'Amore * 124297a3b0SGarrett D'Amore * This code is derived from software contributed to Berkeley by 134297a3b0SGarrett D'Amore * Paul Borman at Krystal Technologies. 144297a3b0SGarrett D'Amore * 154297a3b0SGarrett D'Amore * Redistribution and use in source and binary forms, with or without 164297a3b0SGarrett D'Amore * modification, are permitted provided that the following conditions 174297a3b0SGarrett D'Amore * are met: 184297a3b0SGarrett D'Amore * 1. Redistributions of source code must retain the above copyright 194297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer. 204297a3b0SGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright 214297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer in the 224297a3b0SGarrett D'Amore * documentation and/or other materials provided with the distribution. 234297a3b0SGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors 244297a3b0SGarrett D'Amore * may be used to endorse or promote products derived from this software 254297a3b0SGarrett D'Amore * without specific prior written permission. 264297a3b0SGarrett D'Amore * 274297a3b0SGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 284297a3b0SGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 294297a3b0SGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 304297a3b0SGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 314297a3b0SGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 324297a3b0SGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 334297a3b0SGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 344297a3b0SGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 354297a3b0SGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 364297a3b0SGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 374297a3b0SGarrett D'Amore * SUCH DAMAGE. 384297a3b0SGarrett D'Amore */ 394297a3b0SGarrett D'Amore 404297a3b0SGarrett D'Amore #include "lint.h" 41*2d08521bSGarrett D'Amore #include <locale.h> 424297a3b0SGarrett D'Amore #include <wchar.h> 43*2d08521bSGarrett D'Amore #include <xlocale.h> 444297a3b0SGarrett D'Amore 454297a3b0SGarrett D'Amore int 46*2d08521bSGarrett D'Amore wcswidth_l(const wchar_t *pwcs, size_t n, locale_t loc) 474297a3b0SGarrett D'Amore { 484297a3b0SGarrett D'Amore wchar_t wc; 494297a3b0SGarrett D'Amore int len, l; 504297a3b0SGarrett D'Amore 514297a3b0SGarrett D'Amore len = 0; 524297a3b0SGarrett D'Amore while (n-- > 0 && (wc = *pwcs++) != L'\0') { 53*2d08521bSGarrett D'Amore if ((l = wcwidth_l(wc, loc)) < 0) 544297a3b0SGarrett D'Amore return (-1); 554297a3b0SGarrett D'Amore len += l; 564297a3b0SGarrett D'Amore } 574297a3b0SGarrett D'Amore return (len); 584297a3b0SGarrett D'Amore } 59*2d08521bSGarrett D'Amore 60*2d08521bSGarrett D'Amore int 61*2d08521bSGarrett D'Amore wcswidth(const wchar_t *pwcs, size_t n) 62*2d08521bSGarrett D'Amore { 63*2d08521bSGarrett D'Amore return (wcswidth_l(pwcs, n, uselocale(NULL))); 64*2d08521bSGarrett D'Amore } 65