strdup.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Copyright (C) 1991, 1996, 1997, 1998, 2002, 2003, 2004, 2006, 2007 Free
  2. Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef _LIBC
  16. # include <config.h>
  17. #endif
  18. /* Get specification. */
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #undef __strdup
  22. #ifdef _LIBC
  23. # undef strdup
  24. #endif
  25. #ifndef weak_alias
  26. # define __strdup strdup
  27. #endif
  28. /* Duplicate S, returning an identical malloc'd string. */
  29. char *
  30. __strdup (const char *s)
  31. {
  32. size_t len = strlen (s) + 1;
  33. void *new = malloc (len);
  34. if (new == NULL)
  35. return NULL;
  36. return (char *) memcpy (new, s, len);
  37. }
  38. #ifdef libc_hidden_def
  39. libc_hidden_def (__strdup)
  40. #endif
  41. #ifdef weak_alias
  42. weak_alias (__strdup, strdup)
  43. #endif