Add a wcswidth man page (based on FreeBSD), and fix the implementation
to return -1 in case of an unprintable character.

Index: string/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/string/Makefile.inc,v
retrieving revision 1.23
diff -u -p -r1.23 Makefile.inc
--- string/Makefile.inc 24 Sep 2010 13:33:00 -0000      1.23
+++ string/Makefile.inc 3 Apr 2011 18:44:27 -0000
@@ -144,7 +144,7 @@ MAN+=       bm.3 bcmp.3 bcopy.3 bstring.3 bzer
        strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \
        string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \
        strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3 \
-       wcstok.3 wmemchr.3
+       wcstok.3 wmemchr.3 wcswidth.3
 
 MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3
 MLINKS+=memchr.3 memrchr.3
Index: string/wcswidth.3
===================================================================
RCS file: string/wcswidth.3
diff -N string/wcswidth.3
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ string/wcswidth.3   3 Apr 2011 18:41:48 -0000
@@ -0,0 +1,60 @@
+.\" $OpenBSD$
+.\" Copyright (c) 2002 Tim J. Robbins
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\"
+.Dd $Mdocdate$
+.Dt WCSWIDTH 3
+.Os
+.Sh NAME
+.Nm wcswidth
+.Nd number of column positions in wide-character string
+.Sh SYNOPSIS
+.In wchar.h
+.Ft int
+.Fn wcswidth "const wchar_t *pwcs" "size_t n"
+.Sh DESCRIPTION
+The
+.Fn wcswidth
+function determines the number of column positions required for the first
+.Fa n
+characters of
+.Fa pwcs ,
+or until a null wide character (L'\e0') is encountered.
+.Sh RETURN VALUES
+The
+.Fn wcswidth
+function returns 0 if
+.Fa pwcs
+is an empty string (L""),
+\-1 if a non-printing wide character is encountered,
+otherwise it returns the number of column positions occupied.
+.Sh SEE ALSO
+.Xr iswprint 3 ,
+.Xr wcwidth 3
+.Sh STANDARDS
+The
+.Fn wcswidth
+function conforms to
+.St -p1003.1-2001 .
Index: string/wcswidth.c
===================================================================
RCS file: /cvs/src/lib/libc/string/wcswidth.c,v
retrieving revision 1.3
diff -u -p -r1.3 wcswidth.c
--- string/wcswidth.c   8 Aug 2005 08:05:37 -0000       1.3
+++ string/wcswidth.c   3 Apr 2011 18:44:56 -0000
@@ -34,11 +34,14 @@
 int
 wcswidth(const wchar_t *s, size_t n)
 {
-       int w;
+       int w, q;
 
        w = 0;
        while (n && *s) {
-               w += wcwidth(*s);
+               q = wcwidth(*s);
+               if (q == -1)
+                       return (-1);
+               w += q;
                s++;
                n--;
        }

Reply via email to