Jason Merrill | 6599da0 | 1997-08-21 18:57:35 -0400 | [diff] [blame] | 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 | |
DJ Delorie | aaa5f03 | 2001-09-26 14:16:17 -0400 | [diff] [blame] | 5 | /* |
| 6 | |
| 7 | @deftypefn Replacement char* xstrdup (const char *@var{s}) |
| 8 | |
| 9 | Duplicates a character string without fail, using @code{xmalloc} to |
| 10 | obtain memory. |
| 11 | |
| 12 | @end deftypefn |
| 13 | |
| 14 | */ |
| 15 | |
Kaveh R. Ghazi | 7e4311a | 1999-01-13 11:30:56 +0000 | [diff] [blame] | 16 | #ifdef HAVE_CONFIG_H |
| 17 | #include "config.h" |
| 18 | #endif |
David Edelsohn | 0c9e842 | 2008-10-08 16:51:38 +0000 | [diff] [blame] | 19 | #include <sys/types.h> |
Kaveh R. Ghazi | 7e4311a | 1999-01-13 11:30:56 +0000 | [diff] [blame] | 20 | #ifdef HAVE_STRING_H |
| 21 | #include <string.h> |
Kaveh R. Ghazi | bb99744 | 2005-03-25 04:05:12 +0000 | [diff] [blame] | 22 | #else |
| 23 | # ifdef HAVE_STRINGS_H |
| 24 | # include <strings.h> |
| 25 | # endif |
Kaveh R. Ghazi | 7e4311a | 1999-01-13 11:30:56 +0000 | [diff] [blame] | 26 | #endif |
Jason Merrill | 6599da0 | 1997-08-21 18:57:35 -0400 | [diff] [blame] | 27 | #include "ansidecl.h" |
| 28 | #include "libiberty.h" |
| 29 | |
| 30 | char * |
Gabriel Dos Reis | 7a17ef5 | 2005-03-28 01:28:01 +0000 | [diff] [blame] | 31 | xstrdup (const char *s) |
Jason Merrill | 6599da0 | 1997-08-21 18:57:35 -0400 | [diff] [blame] | 32 | { |
Kaveh R. Ghazi | 7e4311a | 1999-01-13 11:30:56 +0000 | [diff] [blame] | 33 | register size_t len = strlen (s) + 1; |
Gabriel Dos Reis | d7cf839 | 2005-05-24 20:48:25 +0000 | [diff] [blame] | 34 | register char *ret = XNEWVEC (char, len); |
Kaveh R. Ghazi | bb99744 | 2005-03-25 04:05:12 +0000 | [diff] [blame] | 35 | return (char *) memcpy (ret, s, len); |
Jason Merrill | 6599da0 | 1997-08-21 18:57:35 -0400 | [diff] [blame] | 36 | } |