xref: /rk3399_ARM-atf/lib/libc/strnlen.c (revision 72e8f2456af54b75a0a1d92aadfce0b4bcde6ba1)
1c6fdaa73SAntonio Nino Diaz /*-
2c6fdaa73SAntonio Nino Diaz  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3c6fdaa73SAntonio Nino Diaz  *
4c6fdaa73SAntonio Nino Diaz  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
5c6fdaa73SAntonio Nino Diaz  * All rights reserved.
6c6fdaa73SAntonio Nino Diaz  *
7c6fdaa73SAntonio Nino Diaz  * Redistribution and use in source and binary forms, with or without
8c6fdaa73SAntonio Nino Diaz  * modification, are permitted provided that the following conditions
9c6fdaa73SAntonio Nino Diaz  * are met:
10c6fdaa73SAntonio Nino Diaz  * 1. Redistributions of source code must retain the above copyright
11c6fdaa73SAntonio Nino Diaz  *    notice, this list of conditions and the following disclaimer.
12c6fdaa73SAntonio Nino Diaz  * 2. Redistributions in binary form must reproduce the above copyright
13c6fdaa73SAntonio Nino Diaz  *    notice, this list of conditions and the following disclaimer in the
14c6fdaa73SAntonio Nino Diaz  *    documentation and/or other materials provided with the distribution.
15c6fdaa73SAntonio Nino Diaz  *
16c6fdaa73SAntonio Nino Diaz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c6fdaa73SAntonio Nino Diaz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c6fdaa73SAntonio Nino Diaz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c6fdaa73SAntonio Nino Diaz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c6fdaa73SAntonio Nino Diaz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c6fdaa73SAntonio Nino Diaz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c6fdaa73SAntonio Nino Diaz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c6fdaa73SAntonio Nino Diaz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c6fdaa73SAntonio Nino Diaz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c6fdaa73SAntonio Nino Diaz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c6fdaa73SAntonio Nino Diaz  * SUCH DAMAGE.
27c6fdaa73SAntonio Nino Diaz  */
28c6fdaa73SAntonio Nino Diaz 
292c5aca6eSAntonio Nino Diaz /*
30*4c700c15SGovindraj Raja  * Portions copyright (c) 2018, Arm Limited and Contributors.
312c5aca6eSAntonio Nino Diaz  * All rights reserved.
322c5aca6eSAntonio Nino Diaz  */
33c6fdaa73SAntonio Nino Diaz 
34c6fdaa73SAntonio Nino Diaz #include <string.h>
35c6fdaa73SAntonio Nino Diaz 
36c6fdaa73SAntonio Nino Diaz size_t
strnlen(const char * s,size_t maxlen)37c6fdaa73SAntonio Nino Diaz strnlen(const char *s, size_t maxlen)
38c6fdaa73SAntonio Nino Diaz {
39c6fdaa73SAntonio Nino Diaz 	size_t len;
40c6fdaa73SAntonio Nino Diaz 
41c6fdaa73SAntonio Nino Diaz 	for (len = 0; len < maxlen; len++, s++) {
42c6fdaa73SAntonio Nino Diaz 		if (!*s)
43c6fdaa73SAntonio Nino Diaz 			break;
44c6fdaa73SAntonio Nino Diaz 	}
45c6fdaa73SAntonio Nino Diaz 	return (len);
46c6fdaa73SAntonio Nino Diaz }
47