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