view libiberty/strnlen.c @ 158:494b0b89df80 default tip

author Shinji KONO <>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
line wrap: on
line source

/* Portable version of strnlen.
   This function is in the public domain.  */


@deftypefn Supplemental size_t strnlen (const char *@var{s}, size_t @var{maxlen})

Returns the length of @var{s}, as with @code{strlen}, but never looks
past the first @var{maxlen} characters in the string.  If there is no
'\0' character in the first @var{maxlen} characters, returns

@end deftypefn


#include "config.h"

#include <stddef.h>

strnlen (const char *s, size_t maxlen)
  size_t i;

  for (i = 0; i < maxlen; ++i)
    if (s[i] == '\0')
  return i;