2 * Replacement for a missing strlcat.
4 * Provides the same functionality as the *BSD function strlcat, originally
5 * developed by Todd Miller and Theo de Raadt. strlcat works similarly to
6 * strncat, except simpler. The result is always nul-terminated even if the
7 * source string is longer than the space remaining in the destination string,
8 * and the total space required is returned. The third argument is the total
9 * space available in the destination buffer, not just the amount of space
12 * The canonical version of this file is maintained in the rra-c-util package,
13 * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
15 * Written by Russ Allbery <rra@stanford.edu>
17 * The authors hereby relinquish any claim to any copyright that they may have
18 * in this work, whether granted under contract or by operation of law or
19 * international treaty, and hereby commit to the public, at large, that they
20 * shall not, at any time in the future, seek to enforce any copyright in this
21 * work against any person or entity, or prevent any person or entity from
22 * copying, publishing, distributing or creating derivative works of this
27 #include <portable/system.h>
30 * If we're running the test suite, rename strlcat to avoid conflicts with
34 # define strlcat test_strlcat
35 size_t test_strlcat(char *, const char *, size_t);
39 strlcat(char *dst, const char *src, size_t size)
41 size_t used, length, copy;
45 if (size > 0 && used < size - 1) {
46 copy = (length >= size - used) ? size - used - 1 : length;
47 memcpy(dst + used, src, copy);
48 dst[used + copy] = '\0';