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. libc_hidden_proto(strlcpy)
  13. # define Wstrlcpy strlcpy
  14. # define Wstrxfrm strxfrm
  15. #endif
  16. /* OpenBSD function:
  17. * Copy at most n-1 chars from src to dst and nul-terminate dst.
  18. * Returns strlen(src), so truncation occurred if the return value is >= n. */
  19. #ifdef WANT_WIDE
  20. attribute_hidden
  21. #endif
  22. size_t Wstrlcpy(register Wchar *__restrict dst,
  23. register const Wchar *__restrict src,
  24. size_t n)
  25. {
  26. const Wchar *src0 = src;
  27. Wchar dummy[1];
  28. if (!n) {
  29. dst = dummy;
  30. } else {
  31. --n;
  32. }
  33. while ((*dst = *src) != 0) {
  34. if (n) {
  35. --n;
  36. ++dst;
  37. }
  38. ++src;
  39. }
  40. return src - src0;
  41. }
  42. #ifndef WANT_WIDE
  43. libc_hidden_def(strlcpy)
  44. #ifndef __UCLIBC_HAS_LOCALE__
  45. libc_hidden_proto(strxfrm)
  46. strong_alias(strlcpy,strxfrm)
  47. libc_hidden_def(strxfrm)
  48. #endif
  49. #else
  50. #ifndef __UCLIBC_HAS_LOCALE__
  51. strong_alias(__wcslcpy,wcsxfrm)
  52. #endif
  53. #endif