strnlen.c 585 B

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