xref: /optee_os/lib/libutils/isoc/newlib/strnlen.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1*1bb92983SJerome Forissier // SPDX-License-Identifier: BSD-3-Clause
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 1994-2009  Red Hat, Inc.
4b0104773SPascal Brand  * All rights reserved.
5b0104773SPascal Brand  *
6b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
7b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
8b0104773SPascal Brand  *
9b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
10b0104773SPascal Brand  * this list of conditions and the following disclaimer.
11b0104773SPascal Brand  *
12b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
13b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
14b0104773SPascal Brand  * and/or other materials provided with the distribution.
15b0104773SPascal Brand  *
16b0104773SPascal Brand  * 3. Neither the name of the copyright holder nor the names of its
17b0104773SPascal Brand  * contributors may be used to endorse or promote products derived from this
18b0104773SPascal Brand  * software without specific prior written permission.
19b0104773SPascal Brand  *
20b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
31b0104773SPascal Brand  */
32b0104773SPascal Brand 
33b0104773SPascal Brand /*
34b0104773SPascal Brand FUNCTION
35b0104773SPascal Brand 	<<strnlen>>---character string length
36b0104773SPascal Brand 
37b0104773SPascal Brand INDEX
38b0104773SPascal Brand 	strnlen
39b0104773SPascal Brand 
40b0104773SPascal Brand ANSI_SYNOPSIS
41b0104773SPascal Brand 	#include <string.h>
42b0104773SPascal Brand 	size_t strnlen(const char *<[str]>, size_t <[n]>);
43b0104773SPascal Brand 
44b0104773SPascal Brand TRAD_SYNOPSIS
45b0104773SPascal Brand 	#include <string.h>
46b0104773SPascal Brand 	size_t strnlen(<[str]>, <[n]>)
47b0104773SPascal Brand 	char *<[src]>;
48b0104773SPascal Brand 	size_t <[n]>;
49b0104773SPascal Brand 
50b0104773SPascal Brand DESCRIPTION
51b0104773SPascal Brand 	The <<strnlen>> function works out the length of the string
52b0104773SPascal Brand 	starting at <<*<[str]>>> by counting chararacters until it
53b0104773SPascal Brand 	reaches a NUL character or the maximum: <[n]> number of
54b0104773SPascal Brand         characters have been inspected.
55b0104773SPascal Brand 
56b0104773SPascal Brand RETURNS
57b0104773SPascal Brand 	<<strnlen>> returns the character count or <[n]>.
58b0104773SPascal Brand 
59b0104773SPascal Brand PORTABILITY
60b0104773SPascal Brand <<strnlen>> is a GNU extension.
61b0104773SPascal Brand 
62b0104773SPascal Brand <<strnlen>> requires no supporting OS subroutines.
63b0104773SPascal Brand 
64b0104773SPascal Brand */
65b0104773SPascal Brand 
66b0104773SPascal Brand #undef __STRICT_ANSI__
67b0104773SPascal Brand #include "_ansi.h"
68b0104773SPascal Brand #include <string.h>
69b0104773SPascal Brand 
70b0104773SPascal Brand size_t
71b0104773SPascal Brand _DEFUN (strnlen, (str, n),
72b0104773SPascal Brand 	_CONST char *str _AND
73b0104773SPascal Brand 	size_t n)
74b0104773SPascal Brand {
75b0104773SPascal Brand   _CONST char *start = str;
76b0104773SPascal Brand 
77b0104773SPascal Brand   while (n-- > 0 && *str)
78b0104773SPascal Brand     str++;
79b0104773SPascal Brand 
80b0104773SPascal Brand   return str - start;
81b0104773SPascal Brand }
82