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