vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / posix / string / strlcat.c
blob370412030911f122a1e80ec4a0ac46e424ce81b4
1 /*
2 ** Copyright 2002, Manuel J. Petit. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
6 #include <sys/types.h>
7 #include <string.h>
10 /** Concatenates the source string to the destination, writes
11 * as much as "maxLength" bytes to the dest string.
12 * Always null terminates the string as long as maxLength is
13 * larger than the dest string.
14 * Returns the length of the string that it tried to create
15 * to be able to easily detect string truncation.
18 size_t
19 strlcat(char *dest, const char *source, size_t maxLength)
21 size_t destLength = strnlen(dest, maxLength), i;
23 // This returns the wrong size, but it's all we can do
24 if (maxLength == destLength)
25 return destLength + strlen(source);
27 dest += destLength;
28 maxLength -= destLength;
30 for (i = 0; i < maxLength - 1 && source[i]; i++) {
31 dest[i] = source[i];
34 dest[i] = '\0';
36 return destLength + i + strlen(source + i);