]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strcpy.3
strcpy.3: Add LIBRARY section (libc)
[thirdparty/man-pages.git] / man3 / strcpy.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .\" References consulted:
6 .\" Linux libc source code
7 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
8 .\" 386BSD man pages
9 .\" Modified Sat Jul 24 18:06:49 1993 by Rik Faith (faith@cs.unc.edu)
10 .\" Modified Fri Aug 25 23:17:51 1995 by Andries Brouwer (aeb@cwi.nl)
11 .\" Modified Wed Dec 18 00:47:18 1996 by Andries Brouwer (aeb@cwi.nl)
12 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
13 .\" Improve discussion of strncpy().
14 .\"
15 .TH STRCPY 3 2021-03-22 "GNU" "Linux Programmer's Manual"
16 .SH NAME
17 strcpy, strncpy \- copy a string
18 .SH LIBRARY
19 Standard C library
20 .RI ( libc ", " -lc )
21 .SH SYNOPSIS
22 .nf
23 .B #include <string.h>
24 .PP
25 .BI "char *strcpy(char *restrict " dest ", const char *" src );
26 .BI "char *strncpy(char *restrict " dest ", const char *restrict " src \
27 ", size_t " n );
28 .fi
29 .SH DESCRIPTION
30 The
31 .BR strcpy ()
32 function copies the string pointed to by
33 .IR src ,
34 including the terminating null byte (\(aq\e0\(aq),
35 to the buffer pointed to by
36 .IR dest .
37 The strings may not overlap, and the destination string
38 .I dest
39 must be large enough to receive the copy.
40 .IR "Beware of buffer overruns!"
41 (See BUGS.)
42 .PP
43 The
44 .BR strncpy ()
45 function is similar, except that at most
46 .I n
47 bytes of
48 .I src
49 are copied.
50 .BR Warning :
51 If there is no null byte
52 among the first
53 .I n
54 bytes of
55 .IR src ,
56 the string placed in
57 .I dest
58 will not be null-terminated.
59 .PP
60 If the length of
61 .I src
62 is less than
63 .IR n ,
64 .BR strncpy ()
65 writes additional null bytes to
66 .I dest
67 to ensure that a total of
68 .I n
69 bytes are written.
70 .PP
71 A simple implementation of
72 .BR strncpy ()
73 might be:
74 .PP
75 .in +4n
76 .EX
77 char *
78 strncpy(char *dest, const char *src, size_t n)
79 {
80 size_t i;
81
82 for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++)
83 dest[i] = src[i];
84 for ( ; i < n; i++)
85 dest[i] = \(aq\e0\(aq;
86
87 return dest;
88 }
89 .EE
90 .in
91 .SH RETURN VALUE
92 The
93 .BR strcpy ()
94 and
95 .BR strncpy ()
96 functions return a pointer to
97 the destination string
98 .IR dest .
99 .SH ATTRIBUTES
100 For an explanation of the terms used in this section, see
101 .BR attributes (7).
102 .ad l
103 .nh
104 .TS
105 allbox;
106 lbx lb lb
107 l l l.
108 Interface Attribute Value
109 T{
110 .BR strcpy (),
111 .BR strncpy ()
112 T} Thread safety MT-Safe
113 .TE
114 .hy
115 .ad
116 .sp 1
117 .SH CONFORMING TO
118 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
119 .SH NOTES
120 Some programmers consider
121 .BR strncpy ()
122 to be inefficient and error prone.
123 If the programmer knows (i.e., includes code to test!)
124 that the size of
125 .I dest
126 is greater than
127 the length of
128 .IR src ,
129 then
130 .BR strcpy ()
131 can be used.
132 .PP
133 One valid (and intended) use of
134 .BR strncpy ()
135 is to copy a C string to a fixed-length buffer
136 while ensuring both that the buffer is not overflowed
137 and that unused bytes in the destination buffer are zeroed out
138 (perhaps to prevent information leaks if the buffer is to be
139 written to media or transmitted to another process via an
140 interprocess communication technique).
141 .PP
142 If there is no terminating null byte in the first
143 .I n
144 bytes of
145 .IR src ,
146 .BR strncpy ()
147 produces an unterminated string in
148 .IR dest .
149 If
150 .I buf
151 has length
152 .IR buflen ,
153 you can force termination using something like the following:
154 .PP
155 .in +4n
156 .EX
157 if (buflen > 0) {
158 strncpy(buf, str, buflen \- 1);
159 buf[buflen \- 1]= \(aq\e0\(aq;
160 }
161 .EE
162 .in
163 .PP
164 (Of course, the above technique ignores the fact that, if
165 .I src
166 contains more than
167 .I "buflen\ \-\ 1"
168 bytes, information is lost in the copying to
169 .IR dest .)
170 .\"
171 .SS strlcpy()
172 Some systems (the BSDs, Solaris, and others) provide the following function:
173 .PP
174 size_t strlcpy(char *dest, const char *src, size_t size);
175 .PP
176 .\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html
177 .\" "strlcpy and strlcat - consistent, safe, string copy and concatenation"
178 .\" 1999 USENIX Annual Technical Conference
179 This function is similar to
180 .BR strncpy (),
181 but it copies at most
182 .I size\-1
183 bytes to
184 .IR dest ,
185 always adds a terminating null byte,
186 and does not pad the destination with (further) null bytes.
187 This function fixes some of the problems of
188 .BR strcpy ()
189 and
190 .BR strncpy (),
191 but the caller must still handle the possibility of data loss if
192 .I size
193 is too small.
194 The return value of the function is the length of
195 .IR src ,
196 which allows truncation to be easily detected:
197 if the return value is greater than or equal to
198 .IR size ,
199 truncation occurred.
200 If loss of data matters, the caller
201 .I must
202 either check the arguments before the call,
203 or test the function return value.
204 .BR strlcpy ()
205 is not present in glibc and is not standardized by POSIX,
206 .\" https://lwn.net/Articles/506530/
207 but is available on Linux via the
208 .IR libbsd
209 library.
210 .SH BUGS
211 If the destination string of a
212 .BR strcpy ()
213 is not large enough, then anything might happen.
214 Overflowing fixed-length string buffers is a favorite cracker technique
215 for taking complete control of the machine.
216 Any time a program reads or copies data into a buffer,
217 the program first needs to check that there's enough space.
218 This may be unnecessary if you can show that overflow is impossible,
219 but be careful: programs can get changed over time,
220 in ways that may make the impossible possible.
221 .SH SEE ALSO
222 .BR bcopy (3),
223 .BR memccpy (3),
224 .BR memcpy (3),
225 .BR memmove (3),
226 .BR stpcpy (3),
227 .BR stpncpy (3),
228 .BR strdup (3),
229 .BR string (3),
230 .BR wcscpy (3),
231 .BR wcsncpy (3)