strlcpy.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifdef WANT_WIDE
  19. attribute_hidden
  20. #endif
  21. size_t Wstrlcpy(register Wchar *__restrict dst,
  22. register const Wchar *__restrict src,
  23. size_t n)
  24. {
  25. const Wchar *src0 = src;
  26. Wchar dummy[1];
  27. if (!n) {
  28. dst = dummy;
  29. } else {
  30. --n;
  31. }
  32. while ((*dst = *src) != 0) {
  33. if (n) {
  34. --n;
  35. ++dst;
  36. }
  37. ++src;
  38. }
  39. return src - src0;
  40. }
  41. #ifndef WANT_WIDE
  42. libc_hidden_proto(strlcpy)
  43. libc_hidden_def(strlcpy)
  44. #ifndef __UCLIBC_HAS_LOCALE__
  45. strong_alias(strlcpy,strxfrm)
  46. libc_hidden_proto(strxfrm)
  47. libc_hidden_def(strxfrm)
  48. #endif
  49. #else
  50. #ifndef __UCLIBC_HAS_LOCALE__
  51. strong_alias(__wcslcpy,wcsxfrm)
  52. #endif
  53. #endif