1/*
2** Copyright 2002, Manuel J. Petit. All rights reserved.
3** Distributed under the terms of the NewOS License.
4*/
5
6#include <sys/types.h>
7#include <string.h>
8
9
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.
16 */
17
18size_t
19strlcat(char *dest, const char *source, size_t maxLength)
20{
21	size_t destLength = strnlen(dest, maxLength), i;
22
23	// This returns the wrong size, but it's all we can do
24	if (maxLength == destLength)
25		return destLength + strlen(source);
26
27	dest += destLength;
28	maxLength -= destLength;
29
30	for (i = 0; i < maxLength - 1 && source[i]; i++) {
31		dest[i] = source[i];
32	}
33
34	dest[i] = '\0';
35
36	return destLength + i + strlen(source + i);
37}
38
39