strndup.c 434 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. */
  4. #include "portab.h"
  5. /**
  6. * @file
  7. * strndup() implementation. Public domain.
  8. */
  9. #ifndef HAVE_STRNDUP
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <sys/types.h>
  13. GLOBAL char *
  14. strndup(const char *s, size_t maxlen)
  15. {
  16. char *dup;
  17. size_t len = strlen(s);
  18. if (len > maxlen)
  19. len = maxlen;
  20. len++;
  21. dup = malloc(len);
  22. if (dup)
  23. strlcpy(dup, s, len);
  24. return dup;
  25. }
  26. #endif