From af1cb3f2da2d1b806863f3343d8ede2b429dec5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sat, 5 Jul 2003 13:30:06 +0000 Subject: [PATCH] Fixed strlcat(); the old version assumed to have "maxLength" (3rd parameter) bytes space *after* the dest string in the copy loop. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3857 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/libroot/posix/string/strlcat.c | 28 +++++++++++++++-------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/kernel/libroot/posix/string/strlcat.c b/src/kernel/libroot/posix/string/strlcat.c index 68439eb3d7..92a12016e6 100644 --- a/src/kernel/libroot/posix/string/strlcat.c +++ b/src/kernel/libroot/posix/string/strlcat.c @@ -7,21 +7,31 @@ #include +/** Concatenates the source string to the destination, writes + * as much as "maxLength" bytes to the dest string. + * Always null terminates the string as long as maxLength is + * larger than the dest string. + * Returns the length of the string that + */ + size_t -strlcat(char *dst, char const *src, size_t s) +strlcat(char *dest, const char *source, size_t maxLength) { - size_t i, j = strnlen(dst, s); + size_t destLength = strnlen(dest, maxLength), i; - if (!s) - return j + strlen(src); + // This returns the wrong size, but it's all we can do + if (maxLength == destLength) + return destLength + strlen(source); - dst += j; + dest += destLength; + maxLength -= destLength; - for (i = 0; ((i < s-1) && src[i]); i++) { - dst[i] = src[i]; + for (i = 0; i < maxLength - 1 && source[i]; i++) { + dest[i] = source[i]; } - dst[i] = 0; + dest[i] = '\0'; - return j + i + strlen(src + i); + return destLength + i + strlen(source + i); } +