1STRLCPY(3)               BSD Library Functions Manual               STRLCPY(3)
2

NAME

4     strlcpy, strlcat — size-bounded string copying and concatenation
5

LIBRARY

7     Utility functions from BSD systems (libbsd, -lbsd)
8

SYNOPSIS

10     #include <string.h>
11
12     size_t
13     strlcpy(char *dst, const char *src, size_t size);
14
15     size_t
16     strlcat(char *dst, const char *src, size_t size);
17

DESCRIPTION

19     The strlcpy() and strlcat() functions copy and concatenate strings
20     respectively.  They are designed to be safer, more consistent, and less
21     error prone replacements for strncpy(3) and strncat(3).  Unlike those
22     functions, strlcpy() and strlcat() take the full size of the buffer (not
23     just the length) and guarantee to NUL-terminate the result (as long as
24     size is larger than 0 or, in the case of strlcat(), as long as there is
25     at least one byte free in dst).  Note that a byte for the NUL should be
26     included in size.  Also note that strlcpy() and strlcat() only operate on
27     true “C” strings.  This means that for strlcpy() src must be NUL-termi‐
28     nated and for strlcat() both src and dst must be NUL-terminated.
29
30     The strlcpy() function copies up to size - 1 characters from the NUL-ter‐
31     minated string src to dst, NUL-terminating the result.
32
33     The strlcat() function appends the NUL-terminated string src to the end
34     of dst.  It will append at most size - strlen(dst) - 1 bytes, NUL-termi‐
35     nating the result.
36

RETURN VALUES

38     The strlcpy() and strlcat() functions return the total length of the
39     string they tried to create.  For strlcpy() that means the length of src.
40     For strlcat() that means the initial length of dst plus the length of
41     src.  While this may seem somewhat confusing, it was done to make trunca‐
42     tion detection simple.
43
44     Note, however, that if strlcat() traverses size characters without find‐
45     ing a NUL, the length of the string is considered to be size and the des‐
46     tination string will not be NUL-terminated (since there was no space for
47     the NUL).  This keeps strlcat() from running off the end of a string.  In
48     practice this should not happen (as it means that either size is incor‐
49     rect or that dst is not a proper “C” string).  The check exists to pre‐
50     vent potential security problems in incorrect code.
51

EXAMPLES

53     The following code fragment illustrates the simple case:
54
55           char *s, *p, buf[BUFSIZ];
56
57           ...
58
59           (void)strlcpy(buf, s, sizeof(buf));
60           (void)strlcat(buf, p, sizeof(buf));
61
62     To detect truncation, perhaps while building a pathname, something like
63     the following might be used:
64
65           char *dir, *file, pname[MAXPATHLEN];
66
67           ...
68
69           if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
70                   goto toolong;
71           if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
72                   goto toolong;
73
74     Since it is known how many characters were copied the first time, things
75     can be sped up a bit by using a copy instead of an append:
76
77           char *dir, *file, pname[MAXPATHLEN];
78           size_t n;
79
80           ...
81
82           n = strlcpy(pname, dir, sizeof(pname));
83           if (n >= sizeof(pname))
84                   goto toolong;
85           if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
86                   goto toolong;
87
88     However, one may question the validity of such optimizations, as they
89     defeat the whole purpose of strlcpy() and strlcat().  As a matter of
90     fact, the first version of this manual page got it wrong.
91

SEE ALSO

93     snprintf(3), strncat(3), strncpy(3)
94

HISTORY

96     The strlcpy() and strlcat() functions first appeared in OpenBSD 2.4, and
97     made their appearance in FreeBSD 3.3.
98
99BSD                              June 22, 2019                             BSD
Impressum