diff options
Diffstat (limited to 'src/lib/libc/string/strlcpy.3')
| -rw-r--r-- | src/lib/libc/string/strlcpy.3 | 182 |
1 files changed, 182 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..acce0f7d43 --- /dev/null +++ b/src/lib/libc/string/strlcpy.3 | |||
| @@ -0,0 +1,182 @@ | |||
| 1 | .\" $OpenBSD: strlcpy.3,v 1.22 2012/04/03 14:01:55 jmc Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com> | ||
| 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: April 3 2012 $ | ||
| 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 | .Fd #include <string.h> | ||
| 26 | .Ft size_t | ||
| 27 | .Fn strlcpy "char *dst" "const char *src" "size_t dstsize" | ||
| 28 | .Ft size_t | ||
| 29 | .Fn strlcat "char *dst" "const char *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 | .Sh RETURN VALUES | ||
| 84 | Besides quibbles over the return type | ||
| 85 | .Pf ( Va size_t | ||
| 86 | versus | ||
| 87 | .Va int ) | ||
| 88 | and signal handler safety | ||
| 89 | .Pf ( Xr snprintf 3 | ||
| 90 | is not entirely safe on some systems), the | ||
| 91 | following two are equivalent: | ||
| 92 | .Bd -literal -offset indent | ||
| 93 | n = strlcpy(dst, src, len); | ||
| 94 | n = snprintf(dst, len, "%s", src); | ||
| 95 | .Ed | ||
| 96 | .Pp | ||
| 97 | Like | ||
| 98 | .Xr snprintf 3 , | ||
| 99 | the | ||
| 100 | .Fn strlcpy | ||
| 101 | and | ||
| 102 | .Fn strlcat | ||
| 103 | functions return the total length of the string they tried to create. | ||
| 104 | For | ||
| 105 | .Fn strlcpy | ||
| 106 | that means the length of | ||
| 107 | .Fa src . | ||
| 108 | For | ||
| 109 | .Fn strlcat | ||
| 110 | that means the initial length of | ||
| 111 | .Fa dst | ||
| 112 | plus | ||
| 113 | the length of | ||
| 114 | .Fa src . | ||
| 115 | .Pp | ||
| 116 | If the return value is | ||
| 117 | .Cm >= | ||
| 118 | .Va dstsize , | ||
| 119 | the output string has been truncated. | ||
| 120 | It is the caller's responsibility to handle this. | ||
| 121 | .Sh EXAMPLES | ||
| 122 | The following code fragment illustrates the simple case: | ||
| 123 | .Bd -literal -offset indent | ||
| 124 | char *s, *p, buf[BUFSIZ]; | ||
| 125 | |||
| 126 | \&... | ||
| 127 | |||
| 128 | (void)strlcpy(buf, s, sizeof(buf)); | ||
| 129 | (void)strlcat(buf, p, sizeof(buf)); | ||
| 130 | .Ed | ||
| 131 | .Pp | ||
| 132 | To detect truncation, perhaps while building a pathname, something | ||
| 133 | like the following might be used: | ||
| 134 | .Bd -literal -offset indent | ||
| 135 | char *dir, *file, pname[MAXPATHLEN]; | ||
| 136 | |||
| 137 | \&... | ||
| 138 | |||
| 139 | if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname)) | ||
| 140 | goto toolong; | ||
| 141 | if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname)) | ||
| 142 | goto toolong; | ||
| 143 | .Ed | ||
| 144 | .Pp | ||
| 145 | Since it is known how many characters were copied the first time, things | ||
| 146 | can be sped up a bit by using a copy instead of an append: | ||
| 147 | .Bd -literal -offset indent | ||
| 148 | char *dir, *file, pname[MAXPATHLEN]; | ||
| 149 | size_t n; | ||
| 150 | |||
| 151 | \&... | ||
| 152 | |||
| 153 | n = strlcpy(pname, dir, sizeof(pname)); | ||
| 154 | if (n >= sizeof(pname)) | ||
| 155 | goto toolong; | ||
| 156 | if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n) | ||
| 157 | goto toolong; | ||
| 158 | .Ed | ||
| 159 | .Pp | ||
| 160 | However, one may question the validity of such optimizations, as they | ||
| 161 | defeat the whole purpose of | ||
| 162 | .Fn strlcpy | ||
| 163 | and | ||
| 164 | .Fn strlcat . | ||
| 165 | As a matter of fact, the first version of this manual page got it wrong. | ||
| 166 | .Sh SEE ALSO | ||
| 167 | .Xr snprintf 3 , | ||
| 168 | .Xr strncat 3 , | ||
| 169 | .Xr strncpy 3 , | ||
| 170 | .Xr wcslcpy 3 | ||
| 171 | .Sh HISTORY | ||
| 172 | .Fn strlcpy | ||
| 173 | and | ||
| 174 | .Fn strlcat | ||
| 175 | first appeared in | ||
| 176 | .Ox 2.4 . | ||
| 177 | .Sh AUTHORS | ||
| 178 | .Fn strlcpy | ||
| 179 | and | ||
| 180 | .Fn strlcat | ||
| 181 | were created by | ||
| 182 | .An Todd C. Miller Aq Todd.Miller@courtesan.com . | ||
