diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libc/string/strlcpy.3 | 195 |
1 files changed, 0 insertions, 195 deletions
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3 deleted file mode 100644 index 4607289883..0000000000 --- a/src/lib/libc/string/strlcpy.3 +++ /dev/null | |||
@@ -1,195 +0,0 @@ | |||
1 | .\" $OpenBSD: strlcpy.3,v 1.28 2024/08/03 20:13:23 guenther Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1998, 2000 Todd C. Miller <millert@openbsd.org> | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | .\" | ||
17 | .Dd $Mdocdate: August 3 2024 $ | ||
18 | .Dt STRLCPY 3 | ||
19 | .Os | ||
20 | .Sh NAME | ||
21 | .Nm strlcpy , | ||
22 | .Nm strlcat | ||
23 | .Nd size-bounded string copying and concatenation | ||
24 | .Sh SYNOPSIS | ||
25 | .In string.h | ||
26 | .Ft size_t | ||
27 | .Fn strlcpy "char * restrict dst" "const char * restrict src" "size_t dstsize" | ||
28 | .Ft size_t | ||
29 | .Fn strlcat "char * restrict dst" "const char * restrict src" "size_t dstsize" | ||
30 | .Sh DESCRIPTION | ||
31 | The | ||
32 | .Fn strlcpy | ||
33 | and | ||
34 | .Fn strlcat | ||
35 | functions copy and concatenate strings with the | ||
36 | same input parameters and output result as | ||
37 | .Xr snprintf 3 . | ||
38 | They are designed to be safer, more consistent, and less error | ||
39 | prone replacements for the easily misused functions | ||
40 | .Xr strncpy 3 | ||
41 | and | ||
42 | .Xr strncat 3 . | ||
43 | .Pp | ||
44 | .Fn strlcpy | ||
45 | and | ||
46 | .Fn strlcat | ||
47 | take the full size of the destination buffer and guarantee | ||
48 | NUL-termination if there is room. | ||
49 | Note that room for the NUL should be included in | ||
50 | .Fa dstsize . | ||
51 | .Pp | ||
52 | .Fn strlcpy | ||
53 | copies up to | ||
54 | .Fa dstsize | ||
55 | \- 1 characters from the string | ||
56 | .Fa src | ||
57 | to | ||
58 | .Fa dst , | ||
59 | NUL-terminating the result if | ||
60 | .Fa dstsize | ||
61 | is not 0. | ||
62 | .Pp | ||
63 | .Fn strlcat | ||
64 | appends string | ||
65 | .Fa src | ||
66 | to the end of | ||
67 | .Fa dst . | ||
68 | It will append at most | ||
69 | .Fa dstsize | ||
70 | \- strlen(dst) \- 1 characters. | ||
71 | It will then NUL-terminate, unless | ||
72 | .Fa dstsize | ||
73 | is 0 or the original | ||
74 | .Fa dst | ||
75 | string was longer than | ||
76 | .Fa dstsize | ||
77 | (in practice this should not happen | ||
78 | as it means that either | ||
79 | .Fa dstsize | ||
80 | is incorrect or that | ||
81 | .Fa dst | ||
82 | is not a proper string). | ||
83 | .Pp | ||
84 | If the | ||
85 | .Fa src | ||
86 | and | ||
87 | .Fa dst | ||
88 | strings overlap, the behavior is undefined. | ||
89 | .Sh RETURN VALUES | ||
90 | Besides quibbles over the return type | ||
91 | .Pf ( Va size_t | ||
92 | versus | ||
93 | .Va int ) | ||
94 | and signal handler safety | ||
95 | .Pf ( Xr snprintf 3 | ||
96 | is not entirely safe on some systems), the | ||
97 | following two are equivalent: | ||
98 | .Bd -literal -offset indent | ||
99 | n = strlcpy(dst, src, len); | ||
100 | n = snprintf(dst, len, "%s", src); | ||
101 | .Ed | ||
102 | .Pp | ||
103 | Like | ||
104 | .Xr snprintf 3 , | ||
105 | the | ||
106 | .Fn strlcpy | ||
107 | and | ||
108 | .Fn strlcat | ||
109 | functions return the total length of the string they tried to create. | ||
110 | For | ||
111 | .Fn strlcpy | ||
112 | that means the length of | ||
113 | .Fa src . | ||
114 | For | ||
115 | .Fn strlcat | ||
116 | that means the initial length of | ||
117 | .Fa dst | ||
118 | plus | ||
119 | the length of | ||
120 | .Fa src . | ||
121 | .Pp | ||
122 | If the return value is | ||
123 | .Cm >= | ||
124 | .Va dstsize , | ||
125 | the output string has been truncated. | ||
126 | It is the caller's responsibility to handle this. | ||
127 | .Sh EXAMPLES | ||
128 | The following code fragment illustrates the simple case: | ||
129 | .Bd -literal -offset indent | ||
130 | char *s, *p, buf[BUFSIZ]; | ||
131 | |||
132 | \&... | ||
133 | |||
134 | (void)strlcpy(buf, s, sizeof(buf)); | ||
135 | (void)strlcat(buf, p, sizeof(buf)); | ||
136 | .Ed | ||
137 | .Pp | ||
138 | To detect truncation, perhaps while building a pathname, something | ||
139 | like the following might be used: | ||
140 | .Bd -literal -offset indent | ||
141 | char *dir, *file, pname[PATH_MAX]; | ||
142 | |||
143 | \&... | ||
144 | |||
145 | if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname)) | ||
146 | goto toolong; | ||
147 | if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname)) | ||
148 | goto toolong; | ||
149 | .Ed | ||
150 | .Pp | ||
151 | Since it is known how many characters were copied the first time, things | ||
152 | can be sped up a bit by using a copy instead of an append: | ||
153 | .Bd -literal -offset indent | ||
154 | char *dir, *file, pname[PATH_MAX]; | ||
155 | size_t n; | ||
156 | |||
157 | \&... | ||
158 | |||
159 | n = strlcpy(pname, dir, sizeof(pname)); | ||
160 | if (n >= sizeof(pname)) | ||
161 | goto toolong; | ||
162 | if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n) | ||
163 | goto toolong; | ||
164 | .Ed | ||
165 | .Pp | ||
166 | However, one may question the validity of such optimizations, as they | ||
167 | defeat the whole purpose of | ||
168 | .Fn strlcpy | ||
169 | and | ||
170 | .Fn strlcat . | ||
171 | As a matter of fact, the first version of this manual page got it wrong. | ||
172 | .Sh SEE ALSO | ||
173 | .Xr snprintf 3 , | ||
174 | .Xr strncat 3 , | ||
175 | .Xr strncpy 3 , | ||
176 | .Xr wcslcpy 3 | ||
177 | .Sh STANDARDS | ||
178 | The | ||
179 | .Fn strlcat | ||
180 | and | ||
181 | .Fn strlcpy | ||
182 | functions conform to | ||
183 | .St -p1003.1-2024 . | ||
184 | .Sh HISTORY | ||
185 | .Fn strlcpy | ||
186 | and | ||
187 | .Fn strlcat | ||
188 | first appeared in | ||
189 | .Ox 2.4 . | ||
190 | .Sh AUTHORS | ||
191 | .Fn strlcpy | ||
192 | and | ||
193 | .Fn strlcat | ||
194 | were created by | ||
195 | .An Todd C. Miller Aq Mt millert@openbsd.org . | ||