diff options
Diffstat (limited to 'src/lib/libc/string/strncpy.3')
-rw-r--r-- | src/lib/libc/string/strncpy.3 | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/src/lib/libc/string/strncpy.3 b/src/lib/libc/string/strncpy.3 new file mode 100644 index 0000000000..dd8ddb86fc --- /dev/null +++ b/src/lib/libc/string/strncpy.3 | |||
@@ -0,0 +1,153 @@ | |||
1 | .\" $OpenBSD: strncpy.3,v 1.1 2013/12/19 20:52:37 millert 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: December 19 2013 $ | ||
35 | .Dt STRNCPY 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strncpy | ||
39 | .Nd copy part of a string to another | ||
40 | .Sh SYNOPSIS | ||
41 | .In string.h | ||
42 | .Ft char * | ||
43 | .Fn strncpy "char *dst" "const char *src" "size_t len" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strncpy | ||
47 | function copies not more than | ||
48 | .Fa len | ||
49 | characters from the string | ||
50 | .Fa src | ||
51 | to | ||
52 | .Fa dst . | ||
53 | If | ||
54 | .Fa src | ||
55 | is less than | ||
56 | .Fa len | ||
57 | characters long, | ||
58 | it appends | ||
59 | .Ql \e0 | ||
60 | characters for the rest of | ||
61 | .Fa len . | ||
62 | If the length of | ||
63 | .Fa src | ||
64 | is greater than or equal to | ||
65 | .Fa len , | ||
66 | .Fa dst | ||
67 | will | ||
68 | .Em not | ||
69 | be NUL-terminated. | ||
70 | .Pp | ||
71 | If the | ||
72 | .Fa src | ||
73 | and | ||
74 | .Fa dst | ||
75 | strings overlap, the behavior is undefined. | ||
76 | .Sh RETURN VALUES | ||
77 | The | ||
78 | .Fn strncpy | ||
79 | function returns | ||
80 | .Fa dst . | ||
81 | .Sh EXAMPLES | ||
82 | The following sets | ||
83 | .Va chararray | ||
84 | to | ||
85 | .Dq abc\e0\e0\e0 : | ||
86 | .Bd -literal -offset indent | ||
87 | (void)strncpy(chararray, "abc", 6); | ||
88 | .Ed | ||
89 | .Pp | ||
90 | The following sets | ||
91 | .Va chararray | ||
92 | to | ||
93 | .Dq abcdef | ||
94 | and does | ||
95 | .Em not | ||
96 | NUL terminate | ||
97 | .Va chararray | ||
98 | because the length of the source string is greater than or equal to the | ||
99 | length parameter. | ||
100 | .Fn strncpy | ||
101 | .Em only | ||
102 | NUL terminates the destination string when the length of the source | ||
103 | string is less than the length parameter. | ||
104 | .Bd -literal -offset indent | ||
105 | (void)strncpy(chararray, "abcdefgh", 6); | ||
106 | .Ed | ||
107 | .Pp | ||
108 | The following copies as many characters from | ||
109 | .Va input | ||
110 | to | ||
111 | .Va buf | ||
112 | as will fit and NUL terminates the result. | ||
113 | Because | ||
114 | .Fn strncpy | ||
115 | does | ||
116 | .Em not | ||
117 | guarantee to NUL terminate the string itself, it must be done by hand. | ||
118 | .Bd -literal -offset indent | ||
119 | char buf[BUFSIZ]; | ||
120 | |||
121 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
122 | buf[sizeof(buf) - 1] = '\e0'; | ||
123 | .Ed | ||
124 | .Pp | ||
125 | Note that | ||
126 | .Xr strlcpy 3 | ||
127 | is a better choice for this kind of operation. | ||
128 | The equivalent using | ||
129 | .Xr strlcpy 3 | ||
130 | is simply: | ||
131 | .Bd -literal -offset indent | ||
132 | (void)strlcpy(buf, input, sizeof(buf)); | ||
133 | .Ed | ||
134 | .Sh SEE ALSO | ||
135 | .Xr bcopy 3 , | ||
136 | .Xr memccpy 3 , | ||
137 | .Xr memcpy 3 , | ||
138 | .Xr memmove 3 , | ||
139 | .Xr strcat 3 , | ||
140 | .Xr strlcpy 3 , | ||
141 | .Xr strncat 3 , | ||
142 | .Xr wcscpy 3 , | ||
143 | .Xr wcslcpy 3 | ||
144 | .Sh STANDARDS | ||
145 | The | ||
146 | .Fn strncpy | ||
147 | function conforms to | ||
148 | .St -ansiC . | ||
149 | .Sh HISTORY | ||
150 | The | ||
151 | .Fn strncpy | ||
152 | function first appeared in | ||
153 | .At v7 . | ||