stpncpy.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* Implement the stpncpy function.
  2. Copyright (C) 2003, 2011 Free Software Foundation, Inc.
  3. Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. Libiberty is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libiberty; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. /*
  18. @deftypefn Supplemental char* stpncpy (char *@var{dst}, const char *@var{src}, @
  19. size_t @var{len})
  20. Copies the string @var{src} into @var{dst}, copying exactly @var{len}
  21. and padding with zeros if necessary. If @var{len} < strlen(@var{src})
  22. then return @var{dst} + @var{len}, otherwise returns @var{dst} +
  23. strlen(@var{src}).
  24. @end deftypefn
  25. */
  26. #include <ansidecl.h>
  27. #include <stddef.h>
  28. extern size_t strlen (const char *);
  29. extern char *strncpy (char *, const char *, size_t);
  30. char *
  31. stpncpy (char *dst, const char *src, size_t len)
  32. {
  33. size_t n = strlen (src);
  34. if (n > len)
  35. n = len;
  36. return strncpy (dst, src, len) + n;
  37. }