strdup.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Copyright (C) 1991, 1996-1998, 2002-2004, 2006-2007, 2009-2023 Free Software
  2. Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file 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 Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://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