chiark / gitweb /
fix readable logic
[innduct.git] / lib / strlcat.c
1 /*  $Id: strlcat.c 5681 2002-08-29 04:07:50Z rra $
2 **
3 **  Replacement for a missing strlcat.
4 **
5 **  Written by Russ Allbery <rra@stanford.edu>
6 **  This work is hereby placed in the public domain by its author.
7 **
8 **  Provides the same functionality as the *BSD function strlcat, originally
9 **  developed by Todd Miller and Theo de Raadt.  strlcat works similarly to
10 **  strncat, except simpler.  The result is always nul-terminated even if the
11 **  source string is longer than the space remaining in the destination
12 **  string, and the total space required is returned.  The third argument is
13 **  the total space available in the destination buffer, not just the amount
14 **  of space remaining.
15 */
16
17 #include "config.h"
18 #include "clibrary.h"
19
20 /* If we're running the test suite, rename strlcat to avoid conflicts with
21    the system version. */
22 #if TESTING
23 # define strlcat test_strlcat
24 size_t test_strlcat(char *, const char *, size_t);
25 #endif
26
27 size_t
28 strlcat(char *dst, const char *src, size_t size)
29 {
30     size_t used, length, copy;
31
32     used = strlen(dst);
33     length = strlen(src);
34     if (size > 0 && used < size - 1) {
35         copy = (length >= size - used) ? size - used - 1 : length;
36         memcpy(dst + used, src, copy);
37         dst[used + copy] = '\0';
38     }
39     return used + length;
40 }