1 /* Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com> */
2 /*
3 FUNCTION
4 <<strnlen>>---character string length
5
6 INDEX
7 strnlen
8
9 SYNOPSIS
10 #include <string.h>
11 size_t strnlen(const char *<[str]>, size_t <[n]>);
12
13 DESCRIPTION
14 The <<strnlen>> function works out the length of the string
15 starting at <<*<[str]>>> by counting chararacters until it
16 reaches a NUL character or the maximum: <[n]> number of
17 characters have been inspected.
18
19 RETURNS
20 <<strnlen>> returns the character count or <[n]>.
21
22 PORTABILITY
23 <<strnlen>> is a GNU extension.
24
25 <<strnlen>> requires no supporting OS subroutines.
26
27 */
28
29 #undef __STRICT_ANSI__
30 #include <_ansi.h>
31 #include <string.h>
32
33 size_t
strnlen(const char * str,size_t n)34 strnlen (const char *str,
35 size_t n)
36 {
37 const char *start = str;
38
39 while (n-- > 0 && *str)
40 str++;
41
42 return str - start;
43 }
44