diff options
author | millert <> | 1999-03-05 23:16:05 +0000 |
---|---|---|
committer | millert <> | 1999-03-05 23:16:05 +0000 |
commit | a8ea8e7a9fae127a700f870e44831b1d5326b137 (patch) | |
tree | 73d0324d9ad0e344270dab64ba7f43c7df83f131 /src | |
parent | 6a9362f77bbcc30bd34212f35d80f08eb5b436ff (diff) | |
download | openbsd-a8ea8e7a9fae127a700f870e44831b1d5326b137.tar.gz openbsd-a8ea8e7a9fae127a700f870e44831b1d5326b137.tar.bz2 openbsd-a8ea8e7a9fae127a700f870e44831b1d5326b137.zip |
better examples section wrt strncpy()
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/string/strcpy.3 | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3 index ff74d58f8f..0e4804200f 100644 --- a/src/lib/libc/string/strcpy.3 +++ b/src/lib/libc/string/strcpy.3 | |||
@@ -33,7 +33,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 34 | .\" SUCH DAMAGE. |
35 | .\" | 35 | .\" |
36 | .\" $OpenBSD: strcpy.3,v 1.3 1998/11/28 14:51:34 espie Exp $ | 36 | .\" $OpenBSD: strcpy.3,v 1.4 1999/03/05 23:16:05 millert Exp $ |
37 | .\" | 37 | .\" |
38 | .Dd June 29, 1991 | 38 | .Dd June 29, 1991 |
39 | .Dt STRCPY 3 | 39 | .Dt STRCPY 3 |
@@ -96,16 +96,48 @@ The following sets | |||
96 | to | 96 | to |
97 | .Dq Li abc\e0\e0\e0 : | 97 | .Dq Li abc\e0\e0\e0 : |
98 | .Bd -literal -offset indent | 98 | .Bd -literal -offset indent |
99 | (void)strncpy(chararray, "abc", 6). | 99 | (void)strncpy(chararray, "abc", 6); |
100 | .Ed | 100 | .Ed |
101 | .Pp | 101 | .Pp |
102 | The following sets | 102 | The following sets |
103 | .Dq Li chararray | 103 | .Dq Li chararray |
104 | to | 104 | to |
105 | .Dq Li abcdef : | 105 | .Dq Li abcdef |
106 | and does | ||
107 | .Em not | ||
108 | NUL-terminate chararray because the source string is >= the length parameter. | ||
109 | .Fn strncpy | ||
110 | .Em only | ||
111 | NUL-terminates the destination string when then length of the source | ||
112 | string is less than the length parameter. | ||
106 | .Bd -literal -offset indent | 113 | .Bd -literal -offset indent |
107 | (void)strncpy(chararray, "abcdefgh", 6); | 114 | (void)strncpy(chararray, "abcdefgh", 6); |
108 | .Ed | 115 | .Ed |
116 | .Pp | ||
117 | The following copies as many characters from | ||
118 | .Dq Li input | ||
119 | to | ||
120 | .Dq Li buf | ||
121 | as will fit and NUL-terminates the result. Because | ||
122 | .Fn strncpy | ||
123 | does | ||
124 | .Em not | ||
125 | guarantee to NUL-terminate the string itself, we must do this by hand. | ||
126 | .Bd -literal -offset indent | ||
127 | char buf[BUFSIZ]; | ||
128 | |||
129 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
130 | buf[sizeof(buf) - 1] = '\\0'; | ||
131 | .Ed | ||
132 | .Pp | ||
133 | Note that | ||
134 | .Xr strlcpy 3 | ||
135 | is a better choice for this kind of operation. The equivalent using | ||
136 | .Xr strlcpy 3 | ||
137 | is simply: | ||
138 | .Bd -literal -offset indent | ||
139 | (void)strncpy(buf, input, sizeof(buf)); | ||
140 | .Ed | ||
109 | .Sh SEE ALSO | 141 | .Sh SEE ALSO |
110 | .Xr bcopy 3 , | 142 | .Xr bcopy 3 , |
111 | .Xr memccpy 3 , | 143 | .Xr memccpy 3 , |