xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/strnlen.c (revision a2e2270fc5bff6bcbd2c81db532c53a470bd1be5)
1*a2e2270fSchristos /* Portable version of strnlen.
2*a2e2270fSchristos    This function is in the public domain.  */
3*a2e2270fSchristos 
4*a2e2270fSchristos /*
5*a2e2270fSchristos 
6*a2e2270fSchristos @deftypefn Supplemental size_t strnlen (const char *@var{s}, size_t @var{maxlen})
7*a2e2270fSchristos 
8*a2e2270fSchristos Returns the length of @var{s}, as with @code{strlen}, but never looks
9*a2e2270fSchristos past the first @var{maxlen} characters in the string.  If there is no
10*a2e2270fSchristos '\0' character in the first @var{maxlen} characters, returns
11*a2e2270fSchristos @var{maxlen}.
12*a2e2270fSchristos 
13*a2e2270fSchristos @end deftypefn
14*a2e2270fSchristos 
15*a2e2270fSchristos */
16*a2e2270fSchristos 
17*a2e2270fSchristos #include "config.h"
18*a2e2270fSchristos 
19*a2e2270fSchristos #include <stddef.h>
20*a2e2270fSchristos 
21*a2e2270fSchristos size_t
strnlen(const char * s,size_t maxlen)22*a2e2270fSchristos strnlen (const char *s, size_t maxlen)
23*a2e2270fSchristos {
24*a2e2270fSchristos   size_t i;
25*a2e2270fSchristos 
26*a2e2270fSchristos   for (i = 0; i < maxlen; ++i)
27*a2e2270fSchristos     if (s[i] == '\0')
28*a2e2270fSchristos       break;
29*a2e2270fSchristos   return i;
30*a2e2270fSchristos }
31