strndup.c 469 B

12345678910111213141516171819202122232425262728293031323334353637
  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 "imp.h"
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <sys/types.h>
  14. #include "exp.h"
  15. GLOBAL char *
  16. strndup(const char *s, size_t maxlen)
  17. {
  18. char *dup;
  19. size_t len = strlen(s);
  20. if (len > maxlen)
  21. len = maxlen;
  22. len++;
  23. dup = malloc(len);
  24. if (dup)
  25. strlcpy(dup, s, len);
  26. return dup;
  27. }
  28. #endif