xref: /openbsd/lib/libc/string/strnlen.c (revision bf198cc6)
1*bf198cc6Smillert /*	$OpenBSD: strnlen.c,v 1.9 2019/01/25 00:19:25 millert Exp $	*/
2243f3935Stedu 
3243f3935Stedu /*
4*bf198cc6Smillert  * Copyright (c) 2010 Todd C. Miller <millert@openbsd.org>
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
strnlen(const char * str,size_t maxlen)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 
315cc8e0ffSdtucker 	return (size_t)(cp - str);
32243f3935Stedu }
339b9d2a55Sguenther DEF_WEAK(strnlen);
34