Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strnlen.c File Reference
#include <string.h>
Include dependency graph for strnlen.c:

Go to the source code of this file.

Functions

size_t strnlen (const char *str, size_t maxlen)
 Returns the length of the given null-terminated byte string. More...
 

Function Documentation

◆ strnlen()

size_t strnlen ( const char *  str,
size_t  maxlen 
)

Returns the length of the given null-terminated byte string.

Same as

See also
strlen, except that the function returns zero if str is a null pointer and returns maxlen if the null character was not found in the first maxlen bytes of str.

The behavior is undefined if str points to a character array which lacks the null character and the size of that character array < maxlen; in other words, an erroneous value of maxlen does not expose impending buffer overflow.

Parameters
strpointer to the null-terminated byte string to be examined
maxlenmaximum number of characters to examine
Returns
The length of the null-terminated byte string str on success, zero if str is a null pointer, maxlen if the null character was not found.

Definition at line 21 of file strnlen.c.

22 {
23  const char* cp;
24 
25  for(cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
26  {
27  {
28  ;
29  }
30  }
31 
32  return (size_t)(cp - str);
33 }

Referenced by strncat(), and strncpy().

Here is the caller graph for this function: