strndup.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* A replacement function, for systems that lack strndup.
  2. Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2005, 2006 Free
  3. 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 2, 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 "strndup.h"
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "strnlen.h"
  20. char *
  21. strndup (char const *s, size_t n)
  22. {
  23. size_t len = strnlen (s, n);
  24. char *new = malloc (len + 1);
  25. if (new == NULL)
  26. return NULL;
  27. new[len] = '\0';
  28. return memcpy (new, s, len);
  29. }