strdup.c 291 B

12345678910111213141516171819
  1. /* strdup lives in its own object so string.o never pulls in malloc. */
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. char *strdup(const char *s)
  6. {
  7. size_t n;
  8. char *p;
  9. if (!s)
  10. return 0;
  11. n = strlen(s) + 1;
  12. p = (char *)malloc(n);
  13. if (p)
  14. memcpy(p, s, n);
  15. return p;
  16. }