strndup.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /* A replacement function, for systems that lack strndup.
  2. Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2005, 2006, 2007, 2009,
  3. 2010 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. 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
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <config.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. char *
  19. strndup (char const *s, size_t n)
  20. {
  21. size_t len = strnlen (s, n);
  22. char *new = malloc (len + 1);
  23. if (new == NULL)
  24. return NULL;
  25. new[len] = '\0';
  26. return memcpy (new, s, len);
  27. }