strlcpy.c 1.3 KB

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