]> eyrie.org Git - kerberos/krb5-strength.git/blob - portable/strlcat.c
Reorganize the plugin tests
[kerberos/krb5-strength.git] / portable / strlcat.c
1 /*
2  * Replacement for a missing strlcat.
3  *
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
10  * remaining.
11  *
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/>.
14  *
15  * Written by Russ Allbery <rra@stanford.edu>
16  *
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
23  * work.
24  */
25
26 #include <config.h>
27 #include <portable/system.h>
28
29 /*
30  * If we're running the test suite, rename strlcat to avoid conflicts with
31  * the system version.
32  */
33 #if TESTING
34 # define strlcat test_strlcat
35 size_t test_strlcat(char *, const char *, size_t);
36 #endif
37
38 size_t
39 strlcat(char *dst, const char *src, size_t size)
40 {
41     size_t used, length, copy;
42
43     used = strlen(dst);
44     length = strlen(src);
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';
49     }
50     return used + length;
51 }