strlcpy.c 936 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (C) 2002 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include "_string.h"
  8. #ifdef WANT_WIDE
  9. # define Wstrlcpy __wcslcpy
  10. # define Wstrxfrm wcsxfrm
  11. #else
  12. # define Wstrlcpy strlcpy
  13. # define Wstrxfrm strxfrm
  14. #endif
  15. /* OpenBSD function:
  16. * Copy at most n-1 chars from src to dst and nul-terminate dst.
  17. * Returns strlen(src), so truncation occurred if the return value is >= n. */
  18. size_t Wstrlcpy(register Wchar *__restrict dst,
  19. register const Wchar *__restrict src,
  20. size_t n)
  21. {
  22. const Wchar *src0 = src;
  23. Wchar dummy[1];
  24. if (!n) {
  25. dst = dummy;
  26. } else {
  27. --n;
  28. }
  29. while ((*dst = *src) != 0) {
  30. if (n) {
  31. --n;
  32. ++dst;
  33. }
  34. ++src;
  35. }
  36. return src - src0;
  37. }
  38. #ifndef WANT_WIDE
  39. libc_hidden_def(strlcpy)
  40. #endif
  41. #ifndef __UCLIBC_HAS_LOCALE__
  42. strong_alias(Wstrlcpy,Wstrxfrm)
  43. #endif