strlcpy.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 Wstrlcpy wcslcpy
  11. #else
  12. # define __Wstrlcpy __strlcpy
  13. # define Wstrlcpy strlcpy
  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 attribute_hidden __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. strong_alias(__Wstrlcpy,Wstrlcpy)
  39. #ifndef __UCLIBC_HAS_LOCALE__
  40. # ifdef WANT_WIDE
  41. hidden_strong_alias(__wcslcpy,__wcsxfrm)
  42. strong_alias(__wcslcpy,wcsxfrm)
  43. # else
  44. hidden_strong_alias(__strlcpy,__strxfrm)
  45. strong_alias(__strlcpy,strxfrm)
  46. # endif
  47. #endif