strdup.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Copyright (C) 1991, 1996, 1997, 1998, 2002, 2003, 2004, 2006 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 2, 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. /* Get specification. */
  18. # include "strdup.h"
  19. #endif
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #undef __strdup
  23. #ifdef _LIBC
  24. # undef strdup
  25. #endif
  26. #ifndef weak_alias
  27. # define __strdup strdup
  28. #endif
  29. /* Duplicate S, returning an identical malloc'd string. */
  30. char *
  31. __strdup (const char *s)
  32. {
  33. size_t len = strlen (s) + 1;
  34. void *new = malloc (len);
  35. if (new == NULL)
  36. return NULL;
  37. return (char *) memcpy (new, s, len);
  38. }
  39. #ifdef libc_hidden_def
  40. libc_hidden_def (__strdup)
  41. #endif
  42. #ifdef weak_alias
  43. weak_alias (__strdup, strdup)
  44. #endif