xref: /openbsd-src/sys/lib/libkern/strnlen.c (revision bf198cc6eba0ca1f6d79f71e8e2243d386241fa8)
1*bf198cc6Smillert /*	$OpenBSD: strnlen.c,v 1.3 2019/01/25 00:19:26 millert Exp $	*/
2f264d37aSmatthew 
3f264d37aSmatthew /*
4*bf198cc6Smillert  * Copyright (c) 2010 Todd C. Miller <millert@openbsd.org>
5f264d37aSmatthew  *
6f264d37aSmatthew  * Permission to use, copy, modify, and distribute this software for any
7f264d37aSmatthew  * purpose with or without fee is hereby granted, provided that the above
8f264d37aSmatthew  * copyright notice and this permission notice appear in all copies.
9f264d37aSmatthew  *
10f264d37aSmatthew  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11f264d37aSmatthew  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12f264d37aSmatthew  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13f264d37aSmatthew  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14f264d37aSmatthew  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15f264d37aSmatthew  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16f264d37aSmatthew  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17f264d37aSmatthew  */
18f264d37aSmatthew 
19f264d37aSmatthew #include <sys/types.h>
20f264d37aSmatthew #include <lib/libkern/libkern.h>
21f264d37aSmatthew 
22f264d37aSmatthew size_t
strnlen(const char * str,size_t maxlen)23f264d37aSmatthew strnlen(const char *str, size_t maxlen)
24f264d37aSmatthew {
25f264d37aSmatthew 	const char *cp;
26f264d37aSmatthew 
27f264d37aSmatthew 	for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
28f264d37aSmatthew 		;
29f264d37aSmatthew 
30f264d37aSmatthew 	return (size_t)(cp - str);
31f264d37aSmatthew }
32