summaryrefslogtreecommitdiffstats
path: root/binutils-2.25/libiberty/strnlen.c
blob: 4934973adcac15dce91f907915432b7e3b56fe9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* 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
@var{maxlen}.

@end deftypefn

*/

#include "config.h"

#include <stddef.h>

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

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