1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" References consulted:
6 .\" Linux libc source code
7 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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().
15 .TH STRCPY 3 2021-03-22 "GNU" "Linux Programmer's Manual"
17 strcpy, strncpy \- copy a string
20 .RI ( libc ", " \-lc )
23 .B #include <string.h>
25 .BI "char *strcpy(char *restrict " dest ", const char *" src );
26 .BI "char *strncpy(char *restrict " dest ", const char *restrict " src \
32 function copies the string pointed to by
34 including the terminating null byte (\(aq\e0\(aq),
35 to the buffer pointed to by
37 The strings may not overlap, and the destination string
39 must be large enough to receive the copy.
40 .IR "Beware of buffer overruns!"
45 function is similar, except that at most
51 If there is no null byte
58 will not be null-terminated.
65 writes additional null bytes to
67 to ensure that a total of
71 A simple implementation of
78 strncpy(char *dest, const char *src, size_t n)
82 for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++)
85 dest[i] = \(aq\e0\(aq;
96 functions return a pointer to
97 the destination string
100 For an explanation of the terms used in this section, see
108 Interface Attribute Value
112 T} Thread safety MT-Safe
118 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
120 Some programmers consider
122 to be inefficient and error prone.
123 If the programmer knows (i.e., includes code to test!)
133 One valid (and intended) use of
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).
142 If there is no terminating null byte in the first
147 produces an unterminated string in
153 you can force termination using something like the following:
158 strncpy(buf, str, buflen \- 1);
159 buf[buflen \- 1]= \(aq\e0\(aq;
164 (Of course, the above technique ignores the fact that, if
168 bytes, information is lost in the copying to
172 Some systems (the BSDs, Solaris, and others) provide the following function:
174 size_t strlcpy(char *dest, const char *src, size_t size);
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
181 but it copies at most
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
191 but the caller must still handle the possibility of data loss if
194 The return value of the function is the length of
196 which allows truncation to be easily detected:
197 if the return value is greater than or equal to
200 If loss of data matters, the caller
202 either check the arguments before the call,
203 or test the function return value.
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
211 If the destination string of a
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.