strdup.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright (C) 1991, 1996-1998, 2002-2004, 2006-2007, 2009-2013 Free Software
  2. 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, see <http://www.gnu.org/licenses/>. */
  14. #ifndef _LIBC
  15. # include <config.h>
  16. #endif
  17. /* Get specification. */
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #undef __strdup
  21. #ifdef _LIBC
  22. # undef strdup
  23. #endif
  24. #ifndef weak_alias
  25. # define __strdup strdup
  26. #endif
  27. /* Duplicate S, returning an identical malloc'd string. */
  28. char *
  29. __strdup (const char *s)
  30. {
  31. size_t len = strlen (s) + 1;
  32. void *new = malloc (len);
  33. if (new == NULL)
  34. return NULL;
  35. return (char *) memcpy (new, s, len);
  36. }
  37. #ifdef libc_hidden_def
  38. libc_hidden_def (__strdup)
  39. #endif
  40. #ifdef weak_alias
  41. weak_alias (__strdup, strdup)
  42. #endif