diff options
Diffstat (limited to 'src/lib/libc/string/stpcpy.3')
| -rw-r--r-- | src/lib/libc/string/stpcpy.3 | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/src/lib/libc/string/stpcpy.3 b/src/lib/libc/string/stpcpy.3 new file mode 100644 index 0000000000..973eebecdd --- /dev/null +++ b/src/lib/libc/string/stpcpy.3 | |||
| @@ -0,0 +1,184 @@ | |||
| 1 | .\" $OpenBSD: stpcpy.3,v 1.6 2014/02/23 23:09:34 schwarze Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 4 | .\" All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" This code is derived from software contributed to Berkeley by | ||
| 7 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 8 | .\" on Information Processing Systems. | ||
| 9 | .\" | ||
| 10 | .\" Redistribution and use in source and binary forms, with or without | ||
| 11 | .\" modification, are permitted provided that the following conditions | ||
| 12 | .\" are met: | ||
| 13 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer. | ||
| 15 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 17 | .\" documentation and/or other materials provided with the distribution. | ||
| 18 | .\" 3. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .Dd $Mdocdate: February 23 2014 $ | ||
| 35 | .Dt STPCPY 3 | ||
| 36 | .Os | ||
| 37 | .Sh NAME | ||
| 38 | .Nm stpcpy , | ||
| 39 | .Nm stpncpy | ||
| 40 | .Nd copy strings | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .In string.h | ||
| 43 | .Ft char * | ||
| 44 | .Fn stpcpy "char *dst" "const char *src" | ||
| 45 | .Ft char * | ||
| 46 | .Fn stpncpy "char *dst" "const char *src" "size_t len" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn stpcpy | ||
| 50 | and | ||
| 51 | .Fn stpncpy | ||
| 52 | functions copy the string | ||
| 53 | .Fa src | ||
| 54 | to | ||
| 55 | .Fa dst | ||
| 56 | (including the terminating | ||
| 57 | .Ql \e0 | ||
| 58 | character). | ||
| 59 | .Pp | ||
| 60 | The | ||
| 61 | .Fn stpncpy | ||
| 62 | function copies not more than | ||
| 63 | .Fa len | ||
| 64 | characters into | ||
| 65 | .Fa dst , | ||
| 66 | appending | ||
| 67 | .Ql \e0 | ||
| 68 | characters if | ||
| 69 | .Fa src | ||
| 70 | is less than | ||
| 71 | .Fa len | ||
| 72 | characters long, and | ||
| 73 | .Em not | ||
| 74 | terminating | ||
| 75 | .Fa dst | ||
| 76 | if the length of | ||
| 77 | .Fa src | ||
| 78 | is greater than or equal to | ||
| 79 | .Fa len . | ||
| 80 | .Pp | ||
| 81 | If the | ||
| 82 | .Fa src | ||
| 83 | and | ||
| 84 | .Fa dst | ||
| 85 | strings overlap, the behavior is undefined. | ||
| 86 | .Sh RETURN VALUES | ||
| 87 | The | ||
| 88 | .Fn stpcpy | ||
| 89 | function returns a pointer to the terminating | ||
| 90 | .Ql \e0 | ||
| 91 | character written into | ||
| 92 | .Fa dst . | ||
| 93 | .Pp | ||
| 94 | The | ||
| 95 | .Fn stpncpy | ||
| 96 | function returns a pointer to the first | ||
| 97 | .Ql \e0 | ||
| 98 | character written into | ||
| 99 | .Fa dst , | ||
| 100 | or to | ||
| 101 | .Fa &dst[len] | ||
| 102 | if the length of | ||
| 103 | .Fa src | ||
| 104 | is greater than or equal to | ||
| 105 | .Fa len . | ||
| 106 | .Sh EXAMPLES | ||
| 107 | The most common use of | ||
| 108 | .Fn stpcpy | ||
| 109 | is to build up a string from multiple elements. | ||
| 110 | The following example builds up a pathname from | ||
| 111 | directory and file components using | ||
| 112 | .Fn stpcpy : | ||
| 113 | .Bd -literal -offset indent | ||
| 114 | char *dir, *file, pname[PATH_MAX]; | ||
| 115 | |||
| 116 | \&... | ||
| 117 | |||
| 118 | if (strlen(dir) + strlen("/") + strlen(file) >= sizeof(pname)) | ||
| 119 | goto toolong; | ||
| 120 | stpcpy(stpcpy(stpcpy(pname, dir), "/"), file); | ||
| 121 | .Ed | ||
| 122 | .Pp | ||
| 123 | However, the size check required to avoid a buffer overflow is error | ||
| 124 | prone since the check can become out of sync with the code that | ||
| 125 | performs the copy. | ||
| 126 | .Pp | ||
| 127 | One might expect that | ||
| 128 | .Fn stpncpy | ||
| 129 | could be safely used instead, but it suffers from the same defects as | ||
| 130 | .Fn strncpy . | ||
| 131 | The example below using | ||
| 132 | .Fn stpncpy | ||
| 133 | is even more prone to error and will not detect when truncation occurs: | ||
| 134 | .Bd -literal -offset indent | ||
| 135 | char *dir, *file, pname[PATH_MAX]; | ||
| 136 | char *p1, *p2; | ||
| 137 | |||
| 138 | \&... | ||
| 139 | |||
| 140 | p1 = stpncpy(pname, dir, sizeof(pname) - 1); | ||
| 141 | p2 = stpncpy(p1, "/", sizeof(pname) - 1 - (p1 - pname)); | ||
| 142 | stpncpy(p2, file, sizeof(pname) - 1 - (p2 - pname)); | ||
| 143 | pname[sizeof(pname) - 1] = '\e0'; | ||
| 144 | .Ed | ||
| 145 | .Pp | ||
| 146 | A safer (and simpler) approach is to use | ||
| 147 | .Fn snprintf : | ||
| 148 | .Bd -literal -offset indent | ||
| 149 | char *dir, *file, pname[PATH_MAX]; | ||
| 150 | int len; | ||
| 151 | |||
| 152 | \&... | ||
| 153 | |||
| 154 | len = snprintf(pname, sizeof(pname), "%s/%s", dir, file); | ||
| 155 | if (len >= sizeof(pname)) | ||
| 156 | goto toolong; | ||
| 157 | .Ed | ||
| 158 | .Pp | ||
| 159 | In most cases, it is better to use | ||
| 160 | .Fn snprintf , | ||
| 161 | .Fn strlcpy , | ||
| 162 | or | ||
| 163 | .Fn strlcat . | ||
| 164 | .Sh SEE ALSO | ||
| 165 | .Xr snprintf 3 , | ||
| 166 | .Xr strcpy 3 , | ||
| 167 | .Xr strlcpy 3 , | ||
| 168 | .Xr strncpy 3 | ||
| 169 | .Sh STANDARDS | ||
| 170 | The | ||
| 171 | .Fn stpcpy | ||
| 172 | and | ||
| 173 | .Fn stpncpy | ||
| 174 | functions conform to | ||
| 175 | .St -p1003.1-2008 . | ||
| 176 | .Sh HISTORY | ||
| 177 | The function | ||
| 178 | .Fn stpcpy | ||
| 179 | first appeared in the Lattice C AmigaDOS compiler (1986 or earlier). | ||
| 180 | The function | ||
| 181 | .Fn stpncpy | ||
| 182 | first appeared in the GNU C library version 1.07 (1993). | ||
| 183 | Both functions have been available since | ||
| 184 | .Ox 5.1 . | ||
