blob: fa12c96a3cd61e8f7ca919d3dd3ef74989871e3b [file] [log] [blame]
Jason Merrill6599da01997-08-21 18:57:35 -04001/* 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
DJ Delorieaaa5f032001-09-26 14:16:17 -04005/*
6
7@deftypefn Replacement char* xstrdup (const char *@var{s})
8
9Duplicates a character string without fail, using @code{xmalloc} to
10obtain memory.
11
12@end deftypefn
13
14*/
15
Kaveh R. Ghazi7e4311a1999-01-13 11:30:56 +000016#ifdef HAVE_CONFIG_H
17#include "config.h"
18#endif
David Edelsohn0c9e8422008-10-08 16:51:38 +000019#include <sys/types.h>
Kaveh R. Ghazi7e4311a1999-01-13 11:30:56 +000020#ifdef HAVE_STRING_H
21#include <string.h>
Kaveh R. Ghazibb997442005-03-25 04:05:12 +000022#else
23# ifdef HAVE_STRINGS_H
24# include <strings.h>
25# endif
Kaveh R. Ghazi7e4311a1999-01-13 11:30:56 +000026#endif
Jason Merrill6599da01997-08-21 18:57:35 -040027#include "ansidecl.h"
28#include "libiberty.h"
29
30char *
Gabriel Dos Reis7a17ef52005-03-28 01:28:01 +000031xstrdup (const char *s)
Jason Merrill6599da01997-08-21 18:57:35 -040032{
Kaveh R. Ghazi7e4311a1999-01-13 11:30:56 +000033 register size_t len = strlen (s) + 1;
Gabriel Dos Reisd7cf8392005-05-24 20:48:25 +000034 register char *ret = XNEWVEC (char, len);
Kaveh R. Ghazibb997442005-03-25 04:05:12 +000035 return (char *) memcpy (ret, s, len);
Jason Merrill6599da01997-08-21 18:57:35 -040036}