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