strdup.c 562 B

12345678910111213141516171819202122232425262728
  1. /*
  2. @deftypefn Supplemental char* strdup (const char *@var{s})
  3. Returns a pointer to a copy of @var{s} in memory obtained from
  4. @code{malloc}, or @code{NULL} if insufficient memory was available.
  5. @end deftypefn
  6. */
  7. #include <ansidecl.h>
  8. #include <stddef.h>
  9. extern size_t strlen (const char*);
  10. extern PTR malloc (size_t);
  11. extern PTR memcpy (PTR, const PTR, size_t);
  12. char *
  13. strdup(const char *s)
  14. {
  15. size_t len = strlen (s) + 1;
  16. char *result = (char*) malloc (len);
  17. if (result == (char*) 0)
  18. return (char*) 0;
  19. return (char*) memcpy (result, s, len);
  20. }