1strlcpy(3bsd)                        LOCAL                       strlcpy(3bsd)
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     (See libbsd(7) for include usage.)
12
13     size_t
14     strlcpy(char *dst, const char *src, size_t size);
15
16     size_t
17     strlcat(char *dst, const char *src, size_t size);
18

DESCRIPTION

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

RETURN VALUES

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

EXAMPLES

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

SEE ALSO

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

HISTORY

97     The strlcpy() and strlcat() functions first appeared in OpenBSD 2.4, and
98     made their appearance in FreeBSD 3.3.
99
100BSD                              May 31, 2007                              BSD
Impressum