Import wcsnlen(3) to libc

The wcsnlen(3) function conforms to POSIX.1-2008 and is an addition to the
ISO C standard.

size_t wcsnlen(const wchar_t *s, size_t maxlen);

The wcsnlen(3) function computes the number of wide-characters in a wide-
-string to which s points, not including NULL terminating wide-character
code and checking no more than maxlen wide-characters. This function never
examines wide-characters beyond a wide-string of maxlen size.


This function is a safer version of wcslen(3):

size_t wcslen(const wchar_t *s);


Update STANDARDS section of wmemchr(3) describing wide-character functions.
This commit is contained in:
kamil 2016-10-15 14:21:59 +00:00
parent 01368dbcc2
commit 6245abaafa
5 changed files with 122 additions and 19 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.2066 2016/10/14 16:09:45 spz Exp $
# $NetBSD: mi,v 1.2067 2016/10/15 14:21:59 kamil Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
./etc/mtree/set.comp comp-sys-root
@ -9541,6 +9541,7 @@
./usr/share/man/cat3/wcsncat.0 comp-c-catman .cat
./usr/share/man/cat3/wcsncmp.0 comp-c-catman .cat
./usr/share/man/cat3/wcsncpy.0 comp-c-catman .cat
./usr/share/man/cat3/wcsnlen.0 comp-c-catman .cat
./usr/share/man/cat3/wcspbrk.0 comp-c-catman .cat
./usr/share/man/cat3/wcsrchr.0 comp-c-catman .cat
./usr/share/man/cat3/wcsrtombs.0 comp-c-catman .cat
@ -16790,6 +16791,7 @@
./usr/share/man/html3/wcsncat.html comp-c-htmlman html
./usr/share/man/html3/wcsncmp.html comp-c-htmlman html
./usr/share/man/html3/wcsncpy.html comp-c-htmlman html
./usr/share/man/html3/wcsnlen.html comp-c-htmlman html
./usr/share/man/html3/wcspbrk.html comp-c-htmlman html
./usr/share/man/html3/wcsrchr.html comp-c-htmlman html
./usr/share/man/html3/wcsrtombs.html comp-c-htmlman html
@ -24110,6 +24112,7 @@
./usr/share/man/man3/wcsncat.3 comp-c-man .man
./usr/share/man/man3/wcsncmp.3 comp-c-man .man
./usr/share/man/man3/wcsncpy.3 comp-c-man .man
./usr/share/man/man3/wcsnlen.3 comp-c-man .man
./usr/share/man/man3/wcspbrk.3 comp-c-man .man
./usr/share/man/man3/wcsrchr.3 comp-c-man .man
./usr/share/man/man3/wcsrtombs.3 comp-c-man .man

View File

@ -1,4 +1,4 @@
/* $NetBSD: wchar.h,v 1.41 2014/10/13 00:47:03 christos Exp $ */
/* $NetBSD: wchar.h,v 1.42 2016/10/15 14:22:00 kamil Exp $ */
/*-
* Copyright (c)1999 Citrus Project,
@ -116,6 +116,7 @@ size_t wcslen(const wchar_t *);
wchar_t *wcsncat(wchar_t * __restrict, const wchar_t * __restrict, size_t);
int wcsncmp(const wchar_t *, const wchar_t *, size_t);
wchar_t *wcsncpy(wchar_t * __restrict , const wchar_t * __restrict, size_t);
size_t wcsnlen(const wchar_t *, size_t);
wchar_t *wcspbrk(const wchar_t *, const wchar_t *);
wchar_t *wcsrchr(const wchar_t *, wchar_t);
size_t wcsrtombs(char * __restrict, const wchar_t ** __restrict, size_t,

View File

@ -1,5 +1,5 @@
# from: @(#)Makefile.inc 8.1 (Berkeley) 6/4/93
# $NetBSD: Makefile.inc,v 1.81 2016/10/12 20:01:12 christos Exp $
# $NetBSD: Makefile.inc,v 1.82 2016/10/15 14:22:00 kamil Exp $
# string sources
.PATH: ${ARCHDIR}/string ${.CURDIR}/string
@ -27,7 +27,7 @@ SRCS+= popcount32.c popcount64.c
# wide char
SRCS+= wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
wcslen.c wcsncat.c wcscasecmp.c wcsdup.c wcsncasecmp.c \
wcslen.c wcsncat.c wcsnlen.c wcscasecmp.c wcsdup.c wcsncasecmp.c \
wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c wcsstr.c wcstok.c \
wcswcs.c wmemchr.c wmemcmp.c wmemcpy.c wmemmove.c wmemset.c
CPPFLAGS.wcscmp.c+= -I${LIBCDIR}/locale
@ -72,8 +72,9 @@ MLINKS+=wmemchr.3 wmemcmp.3 wmemchr.3 wmemcpy.3 \
wmemchr.3 wcscmp.3 wmemchr.3 wcscpy.3 \
wmemchr.3 wcscspn.3 wmemchr.3 wcslcat.3 \
wmemchr.3 wcslcpy.3 wmemchr.3 wcslen.3 \
wmemchr.3 wcsncat.3 wmemchr.3 wcsncmp.3 \
wmemchr.3 wcsncpy.3 wmemchr.3 wcspbrk.3 \
wmemchr.3 wcsrchr.3 wmemchr.3 wcsspn.3 \
wmemchr.3 wcsstr.3 wmemchr.3 wcswcs.3
wmemchr.3 wcsncat.3 wmemchr.3 wcsnlen.3 \
wmemchr.3 wcsncmp.3 wmemchr.3 wcsncpy.3 \
wmemchr.3 wcspbrk.3 wmemchr.3 wcsrchr.3 \
wmemchr.3 wcsspn.3 wmemchr.3 wcsstr.3 \
wmemchr.3 wcswcs.3
MLINKS+=wcscasecmp.3 wcsncasecmp.3

49
lib/libc/string/wcsnlen.c Normal file
View File

@ -0,0 +1,49 @@
/*-
* Copyright (c) 2016 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Kamil Rytarowski.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: wcsnlen.c,v 1.1 2016/10/15 14:22:00 kamil Exp $");
#include <assert.h>
#include <wchar.h>
size_t
wcsnlen(const wchar_t *s, size_t maxlen)
{
const wchar_t *p;
_DIAGASSERT(s != NULL);
p = s;
while (maxlen-->0 && *p) {
p++;
}
return p - s;
}

View File

@ -1,4 +1,4 @@
.\" $NetBSD: wmemchr.3,v 1.16 2016/07/14 17:17:58 abhinav Exp $
.\" $NetBSD: wmemchr.3,v 1.17 2016/10/15 14:22:00 kamil Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -33,7 +33,7 @@
.\"
.\" from: @(#)strcpy.3 8.1 (Berkeley) 6/4/93
.\"
.Dd May 1, 2009
.Dd October 15, 2016
.Dt WMEMCHR 3
.Os
.Sh NAME
@ -53,6 +53,7 @@
.Nm wcsncat ,
.Nm wcsncmp ,
.Nm wcsncpy ,
.Nm wcsnlen ,
.Nm wcspbrk ,
.Nm wcsrchr ,
.Nm wcsspn ,
@ -95,6 +96,8 @@
.Fn wcsncmp "const wchar_t *s1" "const wchar_t * s2" "size_t n"
.Ft wchar_t *
.Fn wcsncpy "wchar_t * restrict s1" "const wchar_t * restrict s2" "size_t n"
.Ft size_t
.Fn wcsnlen "const wchar_t *s" "size_t maxlen"
.Ft wchar_t *
.Fn wcspbrk "const wchar_t *s1" "const wchar_t *s2"
.Ft wchar_t *
@ -147,16 +150,62 @@ function is strongly recommended to be used.
.Xr strspn 3 ,
.Xr strstr 3
.Sh STANDARDS
These functions conform to
.St -isoC-99
and were first introduced in
.St -isoC-amd1 ,
with the exception of
.Fn wcslcat
The
.Fn wmemchr ,
.Fn wmemcmp ,
.Fn wmemcpy ,
.Fn wmemmove ,
.Fn wmemset ,
.Fn wcscat ,
.Fn wcschr ,
.Fn wcscmp ,
.Fn wcscpy ,
.Fn wcscspn ,
.Fn wcslen ,
.Fn wcsncat ,
.Fn wcsncmp ,
.Fn wcsncpy ,
.Fn wcspbrk ,
.Fn wcsrchr ,
.Fn wcsspn
and
.Fn wcslcpy ,
which are extensions.
.Fn wcsstr
functions were first introduced in
.St -isoC-amd1
and conform to
.St -isoC-99 .
Part of them:
.Fn wmemmove ,
.Fn wcscat ,
.Fn wcscpy ,
.Fn wcsncat
and
.Fn wcsncpy
were modified in
.St -isoC-99
and gained the
.Dv restrict
keyword in parameter list,
this new version is present in
.Nx .
.Pp
The
.Fn wcswcs
function conforms to
.St -xpg4.2 .
.St -xpg4.2 ,
it is recommended to use technically equivalent
.Fn wcsstr
for maximum portability.
.Pp
The
.Fn wcsnlen
function conforms to
.St -p1003.1-2008 .
.Pp
The
.Fn wcslcat
and
.Fn wcslcpy
functions are
.Nx
extensions.