strlcpy.c 1.1 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. #else
  11. # define Wstrlcpy strlcpy
  12. #endif
  13. /* OpenBSD function:
  14. * Copy at most n-1 chars from src to dst and nul-terminate dst.
  15. * Returns strlen(src), so truncation occurred if the return value is >= n. */
  16. #ifdef WANT_WIDE
  17. size_t Wstrlcpy(register Wchar *__restrict dst,
  18. register const Wchar *__restrict src,
  19. size_t n) 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. #ifdef WANT_WIDE
  42. #ifndef __UCLIBC_HAS_LOCALE__
  43. strong_alias(__wcslcpy,wcsxfrm)
  44. libc_hidden_def(wcsxfrm)
  45. #endif
  46. #else
  47. libc_hidden_def(strlcpy)
  48. #ifndef __UCLIBC_HAS_LOCALE__
  49. strong_alias(strlcpy,strxfrm)
  50. libc_hidden_def(strxfrm)
  51. #endif
  52. #endif