strndup.c 650 B

12345678910111213141516171819202122232425262728
  1. /*
  2. * Copyright (C) 2002 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include "_string.h"
  8. #include <stdlib.h>
  9. /* Experimentally off - libc_hidden_proto(strndup) */
  10. /* Experimentally off - libc_hidden_proto(strnlen) */
  11. /* Experimentally off - libc_hidden_proto(memcpy) */
  12. char *strndup(register const char *s1, size_t n)
  13. {
  14. register char *s;
  15. n = strnlen(s1,n); /* Avoid problems if s1 not nul-terminated. */
  16. if ((s = malloc(n + 1)) != NULL) {
  17. memcpy(s, s1, n);
  18. s[n] = 0;
  19. }
  20. return s;
  21. }
  22. libc_hidden_def(strndup)