diff options
author | millert <> | 1998-07-01 01:29:45 +0000 |
---|---|---|
committer | millert <> | 1998-07-01 01:29:45 +0000 |
commit | 919232a14a3b7ddefc332703407abaf2bc94107e (patch) | |
tree | bfbf75abccbb5ddfe2cf2a9d5a1211d80539b1cf /src/lib/libc/string/strlcpy.3 | |
parent | f17e8317cc47a8704164e9ea4730db5b3c45650b (diff) | |
download | openbsd-919232a14a3b7ddefc332703407abaf2bc94107e.tar.gz openbsd-919232a14a3b7ddefc332703407abaf2bc94107e.tar.bz2 openbsd-919232a14a3b7ddefc332703407abaf2bc94107e.zip |
add strlcpy/strlcat, safe and sensible string copy/append
Diffstat (limited to 'src/lib/libc/string/strlcpy.3')
-rw-r--r-- | src/lib/libc/string/strlcpy.3 | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3 new file mode 100644 index 0000000000..e1ff016aaf --- /dev/null +++ b/src/lib/libc/string/strlcpy.3 | |||
@@ -0,0 +1,140 @@ | |||
1 | .\" $OpenBSD: strlcpy.3,v 1.1 1998/07/01 01:29:45 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | ||
4 | .\" All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. The name of the author may not be used to endorse or promote products | ||
15 | .\" derived from this software without specific prior written permission. | ||
16 | .\" | ||
17 | .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
18 | .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
19 | .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
20 | .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
21 | .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
22 | .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
23 | .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
24 | .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
25 | .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
26 | .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
27 | .\" | ||
28 | .Dd June 22, 1998 | ||
29 | .Dt STRLCPY 3 | ||
30 | .Os | ||
31 | .Sh NAME | ||
32 | .Nm strlcpy, | ||
33 | .Nm strlcat | ||
34 | .Nd size-bounded string copying and concatenation | ||
35 | .Sh SYNOPSIS | ||
36 | .Fd #include <string.h> | ||
37 | .Ft char * | ||
38 | .Fn strlcpy "char *dst" "const char *src" "size_t size" | ||
39 | .Ft char * | ||
40 | .Fn strlcat "char *dst" "const char *src" "size_t size" | ||
41 | .Sh DESCRIPTION | ||
42 | The | ||
43 | .Fn strlcpy | ||
44 | and | ||
45 | .Fn strlcat | ||
46 | functions copy and concatenate strings respectively. They are designed | ||
47 | to be safer, more consistent, and less error prone replacements for | ||
48 | .Xr strncpy 3 | ||
49 | and | ||
50 | .Xr strncat 3 . | ||
51 | Unlike those functions, | ||
52 | .Fn strlcpy | ||
53 | and | ||
54 | .Fn strlcat | ||
55 | take the full size of the buffer (not just the length) and guarantee to | ||
56 | NUL-terminate the result (as long as | ||
57 | .Fa size | ||
58 | is larger than 0). Note that you should include a byte for the NUL in | ||
59 | .Fa size . | ||
60 | .Pp | ||
61 | The | ||
62 | .Fn strlcpy | ||
63 | function copies up to | ||
64 | .Fa size | ||
65 | - 1 characters from the NUL-terminated string | ||
66 | .Fa src | ||
67 | to | ||
68 | .Fa dst , | ||
69 | NUL-terminating the result. | ||
70 | .Pp | ||
71 | The | ||
72 | .Fn strlcat | ||
73 | function appends the NUL-terminated string | ||
74 | .Fa src | ||
75 | to the end of | ||
76 | .Fa dst . | ||
77 | It will append at most | ||
78 | .Fa size | ||
79 | - strlen(dst) - 1 bytes, NUL-terminating the result. | ||
80 | .Sh RETURN VALUES | ||
81 | The | ||
82 | .Fn strlcpy | ||
83 | and | ||
84 | .Fn strlcat | ||
85 | functions return the total length of the string they tried to | ||
86 | create. For | ||
87 | .Fn strlcpy | ||
88 | that means the length of | ||
89 | .Fa src . | ||
90 | For | ||
91 | .Fn strlcat | ||
92 | that means the initial length of | ||
93 | .Fa dst | ||
94 | plus | ||
95 | the length of | ||
96 | .Fa src . | ||
97 | While this may seem somewhat confusing it was done to make | ||
98 | truncation detection simple. | ||
99 | .Sh EXAMPLES | ||
100 | The following code fragment illustrates the simple case: | ||
101 | .Bd -literal -offset indent | ||
102 | char *s, *p, buf[BUFSIZ]; | ||
103 | |||
104 | .Li ... | ||
105 | |||
106 | (void)strlcpy(buf, s, sizeof(buf)); | ||
107 | (void)strlcat(buf, p, sizeof(buf)); | ||
108 | .Ed | ||
109 | .Pp | ||
110 | To detect truncation, perhaps while building a pathname, something | ||
111 | like the following might be used: | ||
112 | .Bd -literal -offset indent | ||
113 | char *dir, *file, pname[MAXPATHNAMELEN]; | ||
114 | |||
115 | .Li ... | ||
116 | |||
117 | if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname)) | ||
118 | goto toolong; | ||
119 | if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname)) | ||
120 | goto toolong; | ||
121 | .Ed | ||
122 | .Pp | ||
123 | Since we know how many characters we copied the first time, we can | ||
124 | speed things up a bit by using a copy instead on an append: | ||
125 | .Bd -literal -offset indent | ||
126 | char *dir, *file, pname[MAXPATHNAMELEN]; | ||
127 | size_t n; | ||
128 | |||
129 | .Li ... | ||
130 | |||
131 | n = strlcpy(pname, dir, sizeof(pname)); | ||
132 | if (n >= sizeof(pname)) | ||
133 | goto toolong; | ||
134 | if (strlcpy(pname + n, file, sizeof(pname)) >= sizeof(pname) - n) | ||
135 | goto toolong; | ||
136 | .Ed | ||
137 | .Sh SEE ALSO | ||
138 | .Xr snprintf 3 , | ||
139 | .Xr strncpy 3 , | ||
140 | .Xr strncat 3 | ||