strnlen.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* Find the length of STRING, but scan at most MAXLEN characters.
  2. Copyright (C) 1991-2026 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, see <https://www.gnu.org/licenses/>. */
  14. #include <string.h>
  15. /* Find the length of S, but scan at most MAXLEN characters. If no
  16. '\0' terminator is found in that many characters, return MAXLEN. */
  17. #ifdef STRNLEN
  18. # define __strnlen STRNLEN
  19. #endif
  20. size_t
  21. __strnlen (const char *str, size_t maxlen)
  22. {
  23. const char *found = memchr (str, '\0', maxlen);
  24. return found ? found - str : maxlen;
  25. }
  26. #ifndef STRNLEN
  27. weak_alias (__strnlen, strnlen)
  28. libc_hidden_def (__strnlen)
  29. libc_hidden_def (strnlen)
  30. #endif