Retro68/gcc/newlib/libc/string/strnlen.c

43 lines
744 B
C
Raw Normal View History

2017-04-11 21:13:36 +00:00
/*
FUNCTION
<<strnlen>>---character string length
INDEX
strnlen
2018-12-28 15:30:48 +00:00
SYNOPSIS
2017-04-11 21:13:36 +00:00
#include <string.h>
size_t strnlen(const char *<[str]>, size_t <[n]>);
DESCRIPTION
The <<strnlen>> function works out the length of the string
starting at <<*<[str]>>> by counting chararacters until it
reaches a NUL character or the maximum: <[n]> number of
characters have been inspected.
RETURNS
<<strnlen>> returns the character count or <[n]>.
PORTABILITY
<<strnlen>> is a GNU extension.
<<strnlen>> requires no supporting OS subroutines.
*/
#undef __STRICT_ANSI__
#include <_ansi.h>
#include <string.h>
size_t
2018-12-28 15:30:48 +00:00
strnlen (const char *str,
2017-04-11 21:13:36 +00:00
size_t n)
{
2018-12-28 15:30:48 +00:00
const char *start = str;
2017-04-11 21:13:36 +00:00
while (n-- > 0 && *str)
str++;
return str - start;
}