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