I plan to use this in a subsequent AF_UNIX cleanup diff. ok?
Index: lib/libc/Makefile.inc =================================================================== RCS file: /home/mdempsky/anoncvs/cvs/src/lib/libc/Makefile.inc,v retrieving revision 1.16 diff -u -p -r1.16 Makefile.inc --- lib/libc/Makefile.inc 24 Sep 2010 13:33:00 -0000 1.16 +++ lib/libc/Makefile.inc 25 Apr 2012 23:02:34 -0000 @@ -61,8 +61,8 @@ CFLAGS+=-DNLS LIBKERN= ${LIBCSRCDIR}/../../sys/lib/libkern -KSRCS= bcmp.c bzero.c ffs.c strcat.c strcmp.c strcpy.c strlen.c strncmp.c \ - strncpy.c htonl.c htons.c ntohl.c ntohs.c timingsafe_bcmp.c +KSRCS= bcmp.c bzero.c ffs.c strcat.c strcmp.c strcpy.c strlen.c strncmp.c \ + strncpy.c strnlen.c htonl.c htons.c ntohl.c ntohs.c timingsafe_bcmp.c .if (${MACHINE_CPU} != "alpha") KSRCS+= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c iordi3.c \ lshldi3.c lshrdi3.c moddi3.c muldi3.c negdi2.c notdi2.c qdivrem.c \ Index: lib/libc/string/strnlen.c =================================================================== RCS file: /home/mdempsky/anoncvs/cvs/src/lib/libc/string/strnlen.c,v retrieving revision 1.3 diff -u -p -r1.3 strnlen.c --- lib/libc/string/strnlen.c 2 Jun 2010 12:58:12 -0000 1.3 +++ lib/libc/string/strnlen.c 25 Apr 2012 23:04:50 -0000 @@ -18,7 +18,11 @@ #include <sys/types.h> +#if !defined(_KERNEL) && !defined(_STANDALONE) #include <string.h> +#else +#include <lib/libkern/libkern.h> +#endif size_t strnlen(const char *str, size_t maxlen) Index: sys/conf/files =================================================================== RCS file: /home/mdempsky/anoncvs/cvs/src/sys/conf/files,v retrieving revision 1.536 diff -u -p -r1.536 files --- sys/conf/files 6 Apr 2012 15:10:40 -0000 1.536 +++ sys/conf/files 25 Apr 2012 23:06:04 -0000 @@ -1034,6 +1034,7 @@ file lib/libkern/arch/${MACHINE_ARCH}/st file lib/libkern/arch/${MACHINE_ARCH}/strlen.S | lib/libkern/strlen.c file lib/libkern/arch/${MACHINE_ARCH}/strncmp.S | lib/libkern/strncmp.c file lib/libkern/arch/${MACHINE_ARCH}/strncpy.S | lib/libkern/strncpy.c +file lib/libkern/arch/${MACHINE_ARCH}/strnlen.S | lib/libkern/strnlen.c file lib/libkern/arch/${MACHINE_ARCH}/scanc.S | lib/libkern/scanc.c file lib/libkern/arch/${MACHINE_ARCH}/skpc.S | lib/libkern/skpc.c file lib/libkern/arch/${MACHINE_ARCH}/htonl.S | lib/libkern/htonl.c Index: sys/lib/libkern/strnlen.c =================================================================== RCS file: sys/lib/libkern/strnlen.c diff -N sys/lib/libkern/strnlen.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ sys/lib/libkern/strnlen.c 25 Apr 2012 23:04:50 -0000 @@ -0,0 +1,36 @@ +/* $OpenBSD: strnlen.c,v 1.3 2010/06/02 12:58:12 millert Exp $ */ + +/* + * Copyright (c) 2010 Todd C. Miller <todd.mil...@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> + +#if !defined(_KERNEL) && !defined(_STANDALONE) +#include <string.h> +#else +#include <lib/libkern/libkern.h> +#endif + +size_t +strnlen(const char *str, size_t maxlen) +{ + const char *cp; + + for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) + ; + + return (size_t)(cp - str); +} Index: share/man/man9/kern.9 =================================================================== RCS file: /home/mdempsky/anoncvs/cvs/src/share/man/man9/kern.9,v retrieving revision 1.14 diff -u -p -r1.14 kern.9 --- share/man/man9/kern.9 9 Jan 2011 02:26:31 -0000 1.14 +++ share/man/man9/kern.9 26 Apr 2012 01:07:16 -0000 @@ -164,6 +164,8 @@ and .Ft char * .Fn strncpy "char *dst" "const char *src" "size_t len" .Ft size_t +.Fn strnlen "const char *s" "size_t maxlen" +.Ft size_t .Fn strlcpy "char *dst" "const char *src" "size_t size" .Ft size_t .Fn strlcat "char *dst" "const char *src" "size_t size" @@ -178,6 +180,7 @@ and Those functions have the same semantics as their libc counterparts, .Xr strlen 3 , .Xr strncpy 3 , +.Xr strnlen 3 , .Xr strlcpy 3 , .Xr strlcat 3 , .Xr strcmp 3 , Index: share/man/man9/Makefile =================================================================== RCS file: /home/mdempsky/anoncvs/cvs/src/share/man/man9/Makefile,v retrieving revision 1.169 diff -u -p -r1.169 Makefile --- share/man/man9/Makefile 8 Dec 2011 20:58:49 -0000 1.169 +++ share/man/man9/Makefile 26 Apr 2012 01:07:53 -0000 @@ -184,6 +184,7 @@ MLINKS+=kern.9 imax.9 kern.9 imin.9 kern kern.9 memchr.9 kern.9 memcmp.9 kern.9 ffs.9 \ kern.9 strlen.9 kern.9 strncpy.9 kern.9 strlcpy.9 kern.9 strlcat.9 \ kern.9 strcmp.9 kern.9 strncmp.9 kern.9 strncasecmp.9 \ + kern.9 strnlen.9 \ kern.9 timingsafe_bcmp.9 kern.9 getsn.9 MLINKS+=km_alloc.9 km_free.9 MLINKS+=knote.9 KNOTE.9