xref: /openbsd/lib/libc/string/strnlen.c (revision 9b9d2a55)
1*9b9d2a55Sguenther /*	$OpenBSD: strnlen.c,v 1.6 2015/08/31 02:53:57 guenther Exp $	*/
2243f3935Stedu 
3243f3935Stedu /*
4243f3935Stedu  * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
5243f3935Stedu  *
6243f3935Stedu  * Permission to use, copy, modify, and distribute this software for any
7243f3935Stedu  * purpose with or without fee is hereby granted, provided that the above
8243f3935Stedu  * copyright notice and this permission notice appear in all copies.
9243f3935Stedu  *
10243f3935Stedu  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11243f3935Stedu  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12243f3935Stedu  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13243f3935Stedu  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14243f3935Stedu  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15243f3935Stedu  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16243f3935Stedu  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17243f3935Stedu  */
18243f3935Stedu 
19243f3935Stedu #include <sys/types.h>
20243f3935Stedu 
21243f3935Stedu #include <string.h>
22243f3935Stedu 
23243f3935Stedu size_t
24243f3935Stedu strnlen(const char *str, size_t maxlen)
25243f3935Stedu {
26d2b9bf02Smillert 	const char *cp;
27243f3935Stedu 
28d2b9bf02Smillert 	for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
29243f3935Stedu 		;
30243f3935Stedu 
31243f3935Stedu 	return (size_t)(cp - str);
32243f3935Stedu }
33*9b9d2a55Sguenther DEF_WEAK(strnlen);
34