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