strlcat.c 867 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. /* OpenBSD function:
  8. * Append at most n-1-strlen(dst) chars from src to dst and nul-terminate dst.
  9. * Returns strlen(src) + strlen({original} dst), so truncation occurred if the
  10. * return val is >= n.
  11. * Note: If dst doesn't contain a nul in the first n chars, strlen(dst) is
  12. * taken as n. */
  13. #include "_string.h"
  14. size_t strlcat(register char *__restrict dst,
  15. register const char *__restrict src,
  16. size_t n)
  17. {
  18. size_t len;
  19. char dummy[1];
  20. len = 0;
  21. while (1) {
  22. if (len >= n) {
  23. dst = dummy;
  24. break;
  25. }
  26. if (!*dst) {
  27. break;
  28. }
  29. ++dst;
  30. ++len;
  31. }
  32. while ((*dst = *src) != 0) {
  33. if (++len < n) {
  34. ++dst;
  35. }
  36. ++src;
  37. }
  38. return len;
  39. }