xstrdup.c 720 B

123456789101112131415161718192021222324252627282930313233343536
  1. /* xstrdup.c -- Duplicate a string in memory, using xmalloc.
  2. This trivial function is in the public domain.
  3. Ian Lance Taylor, Cygnus Support, December 1995. */
  4. /*
  5. @deftypefn Replacement char* xstrdup (const char *@var{s})
  6. Duplicates a character string without fail, using @code{xmalloc} to
  7. obtain memory.
  8. @end deftypefn
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include <sys/types.h>
  14. #ifdef HAVE_STRING_H
  15. #include <string.h>
  16. #else
  17. # ifdef HAVE_STRINGS_H
  18. # include <strings.h>
  19. # endif
  20. #endif
  21. #include "ansidecl.h"
  22. #include "libiberty.h"
  23. char *
  24. xstrdup (const char *s)
  25. {
  26. register size_t len = strlen (s) + 1;
  27. register char *ret = XNEWVEC (char, len);
  28. return (char *) memcpy (ret, s, len);
  29. }