summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjca <>2024-07-14 09:51:18 +0000
committerjca <>2024-07-14 09:51:18 +0000
commit1321c907704edb09732b01534fbb20795096f774 (patch)
treed0b85a6e9f134baeb802f6d3e37e8de3e4f5ba43
parent6e4e3468814a1cc69459616c847470c795145c56 (diff)
downloadopenbsd-1321c907704edb09732b01534fbb20795096f774.tar.gz
openbsd-1321c907704edb09732b01534fbb20795096f774.tar.bz2
openbsd-1321c907704edb09732b01534fbb20795096f774.zip
Add wcsnlen(3), wcslen(3) with a max len argument
Missing function hit by fcambus@ some time ago. ok millert@
-rw-r--r--src/lib/libc/string/Makefile.inc8
-rw-r--r--src/lib/libc/string/wcslen.338
-rw-r--r--src/lib/libc/string/wcsnlen.c45
3 files changed, 84 insertions, 7 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
index a1b1934aad..204ca1b266 100644
--- a/src/lib/libc/string/Makefile.inc
+++ b/src/lib/libc/string/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.39 2017/09/05 03:16:13 schwarze Exp $ 1# $OpenBSD: Makefile.inc,v 1.40 2024/07/14 09:51:18 jca Exp $
2 2
3# string sources 3# string sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string
@@ -9,9 +9,9 @@ SRCS+= explicit_bzero.c memccpy.c memmem.c memrchr.c stpcpy.c stpncpy.c \
9 strndup.c strnlen.c strsignal.c strtok.c strxfrm.c strxfrm_l.c \ 9 strndup.c strnlen.c strsignal.c strtok.c strxfrm.c strxfrm_l.c \
10 timingsafe_bcmp.c timingsafe_memcmp.c \ 10 timingsafe_bcmp.c timingsafe_memcmp.c \
11 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ 11 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
12 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \ 12 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcsnlen.c wcspbrk.c wcsrchr.c \
13 wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \ 13 wcsspn.c wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c \
14 wmemmove.c wmemset.c wcsdup.c wcscasecmp.c wcscasecmp_l.c 14 wmemcpy.c wmemmove.c wmemset.c wcsdup.c wcscasecmp.c wcscasecmp_l.c
15 15
16# machine-dependent net sources 16# machine-dependent net sources
17# ../arch/ARCH/Makefile.inc must include sources for: 17# ../arch/ARCH/Makefile.inc must include sources for:
diff --git a/src/lib/libc/string/wcslen.3 b/src/lib/libc/string/wcslen.3
index 12f81763ba..b6d9aafc41 100644
--- a/src/lib/libc/string/wcslen.3
+++ b/src/lib/libc/string/wcslen.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: wcslen.3,v 1.3 2013/06/05 03:39:23 tedu Exp $ 1.\" $OpenBSD: wcslen.3,v 1.4 2024/07/14 09:51:18 jca Exp $
2.\" 2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California. 3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved. 4.\" All rights reserved.
@@ -31,26 +31,50 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.Dd $Mdocdate: June 5 2013 $ 34.Dd $Mdocdate: July 14 2024 $
35.Dt WCSLEN 3 35.Dt WCSLEN 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm wcslen 38.Nm wcslen ,
39.Nm wcsnlen
39.Nd find length of a wide string 40.Nd find length of a wide string
40.Sh SYNOPSIS 41.Sh SYNOPSIS
41.In wchar.h 42.In wchar.h
42.Ft size_t 43.Ft size_t
43.Fn wcslen "const wchar_t *s" 44.Fn wcslen "const wchar_t *s"
45.Ft size_t
46.Fn wcsnlen "const wchar_t *s" "size_t maxlen"
44.Sh DESCRIPTION 47.Sh DESCRIPTION
45The 48The
46.Fn wcslen 49.Fn wcslen
47function computes the length of the wide string 50function computes the length of the wide string
48.Fa s . 51.Fa s .
52The
53.Fn wcsnlen
54function computes the length of the wide string
55.Fa s ,
56up to
57.Fa maxlen
58wide characters.
59The
60.Fn wcsnlen
61function will never attempt to address more than
62.Fa maxlen
63wide characters, making it suitable for use with wide character arrays
64that are not guaranteed to be NUL-terminated.
49.Sh RETURN VALUES 65.Sh RETURN VALUES
50The 66The
51.Fn wcslen 67.Fn wcslen
52function returns the number of wide characters that precede the terminating 68function returns the number of wide characters that precede the terminating
53null wide character. 69null wide character.
70.Pp
71The
72.Fn wcsnlen
73function returns the number of wide characters that precede the terminating
74null wide character
75or
76.Fa maxlen ,
77whichever is smaller.
54.Sh SEE ALSO 78.Sh SEE ALSO
55.Xr strlen 3 , 79.Xr strlen 3 ,
56.Xr wcswidth 3 80.Xr wcswidth 3
@@ -61,6 +85,10 @@ function conforms to
61.St -isoC-99 85.St -isoC-99
62and was first introduced in 86and was first introduced in
63.St -isoC-amd1 . 87.St -isoC-amd1 .
88The
89.Fn wcsnlen
90function conforms to
91.St -p1003.1-2008 .
64.Sh HISTORY 92.Sh HISTORY
65The 93The
66.Fn wcslen 94.Fn wcslen
@@ -68,3 +96,7 @@ function was ported from
68.Nx 96.Nx
69and first appeared in 97and first appeared in
70.Ox 3.8 . 98.Ox 3.8 .
99The
100.Fn wcsnlen
101function first appeared in
102.Ox 7.6 .
diff --git a/src/lib/libc/string/wcsnlen.c b/src/lib/libc/string/wcsnlen.c
new file mode 100644
index 0000000000..eb1d08e85b
--- /dev/null
+++ b/src/lib/libc/string/wcsnlen.c
@@ -0,0 +1,45 @@
1/* $OpenBSD: wcsnlen.c,v 1.1 2024/07/14 09:51:18 jca Exp $ */
2/* $NetBSD: wcslen.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcslen.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp
30 */
31
32#include <wchar.h>
33
34size_t
35wcsnlen(const wchar_t *s, size_t maxlen)
36{
37 const wchar_t *p;
38
39 p = s;
40 while (maxlen-- && *p)
41 p++;
42
43 return p - s;
44}
45