]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strcpy.3
intro.1, _exit.2, access.2, alarm.2, alloc_hugepages.2, arch_prctl.2, bind.2, chdir...
[thirdparty/man-pages.git] / man3 / strcpy.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(verbatim)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" 386BSD man pages
29 .\" Modified Sat Jul 24 18:06:49 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" Modified Fri Aug 25 23:17:51 1995 by Andries Brouwer (aeb@cwi.nl)
31 .\" Modified Wed Dec 18 00:47:18 1996 by Andries Brouwer (aeb@cwi.nl)
32 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
33 .\" Improve discussion of strncpy().
34 .\"
35 .TH STRCPY 3 2012-07-19 "GNU" "Linux Programmer's Manual"
36 .SH NAME
37 strcpy, strncpy \- copy a string
38 .SH SYNOPSIS
39 .nf
40 .B #include <string.h>
41 .sp
42 .BI "char *strcpy(char *" dest ", const char *" src );
43 .sp
44 .BI "char *strncpy(char *" dest ", const char *" src ", size_t " n );
45 .fi
46 .SH DESCRIPTION
47 The
48 .BR strcpy ()
49 function copies the string pointed to by \fIsrc\fP,
50 including the terminating null byte (\(aq\\0\(aq),
51 to the buffer pointed to by \fIdest\fP.
52 The strings may not overlap, and the destination string
53 \fIdest\fP must be large enough to receive the copy.
54 .IR "Beware of buffer overruns!"
55 (See BUGS.)
56 .PP
57 The
58 .BR strncpy ()
59 function is similar, except that at most
60 \fIn\fP bytes of \fIsrc\fP are copied.
61 .BR Warning :
62 If there is no null byte
63 among the first \fIn\fP bytes of \fIsrc\fP,
64 the string placed in \fIdest\fP will not be null-terminated.
65 .PP
66 If the length of
67 .I src
68 is less than
69 .IR n ,
70 .BR strncpy ()
71 writes additional null bytes to
72 .I dest
73 to ensure that a total of
74 .I n
75 bytes are written.
76 .PP
77 A simple implementation of
78 .BR strncpy ()
79 might be:
80 .in +4n
81 .nf
82
83 char *
84 strncpy(char *dest, const char *src, size_t n)
85 {
86 size_t i;
87
88 for (i = 0; i < n && src[i] != \(aq\\0\(aq; i++)
89 dest[i] = src[i];
90 for ( ; i < n; i++)
91 dest[i] = \(aq\\0\(aq;
92
93 return dest;
94 }
95 .fi
96 .in
97 .SH RETURN VALUE
98 The
99 .BR strcpy ()
100 and
101 .BR strncpy ()
102 functions return a pointer to
103 the destination string \fIdest\fP.
104 .SH CONFORMING TO
105 SVr4, 4.3BSD, C89, C99.
106 .SH NOTES
107 Some programmers consider
108 .BR strncpy ()
109 to be inefficient and error prone.
110 If the programmer knows (i.e., includes code to test!)
111 that the size of \fIdest\fP is greater than
112 the length of \fIsrc\fP, then
113 .BR strcpy ()
114 can be used.
115
116 One valid (and intended) use of
117 .BR strncpy ()
118 is to copy a C string to a fixed-length buffer
119 while ensuring both that the buffer is not overflowed
120 and that unused bytes in the target buffer are zeroed out
121 (perhaps to prevent information leaks if the buffer is to be
122 written to media or transmitted to another process via an
123 interprocess communication technique).
124
125 If there is no terminating null byte in the first \fIn\fP
126 bytes of \fIsrc\fP,
127 .BR strncpy ()
128 produces an unterminated string in \fIdest\fP.
129 You can force termination using something like the following:
130 .in +4n
131 .nf
132
133 strncpy(buf, str, n);
134 if (n > 0)
135 buf[n \- 1]= \(aq\\0\(aq;
136 .fi
137 .in
138 .PP
139 (Of course, the above technique ignores the fact that
140 information contained in
141 .I src
142 is lost in the copying to
143 .IR dest .)
144
145 Some systems (the BSDs, Solaris, and others) provide the following function:
146
147 size_t strlcpy(char *dest, const char *src, size_t size);
148
149 .\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html
150 .\" "strlcpy and strlcat - consistent, safe, string copy and concatenation"
151 .\" 1999 USENIX Annual Technical Conference
152 This function is similar to
153 .BR strncpy (),
154 but it copies at most
155 .I size\-1
156 bytes to
157 .IR dest ,
158 always adds a terminating null byte,
159 and does not pad the target with (further) null bytes.
160 This function fixes some of the problems of
161 .BR strcpy ()
162 and
163 .BR strncpy (),
164 but the caller must still handle the possibility of data loss if
165 .I size
166 is too small.
167 The return value of the function is the length of
168 .IR src ,
169 which allows truncation to be easily detected:
170 if the return value is greater than or equal to
171 .IR size ,
172 truncation occurred.
173 If loss of data matters, the caller
174 .I must
175 either check the arguments before the call,
176 or test the function return value.
177 .BR strlcpy ()
178 is not present in glibc and is not standardized by POSIX,
179 .\" https://lwn.net/Articles/506530/
180 but is available on Linux via the
181 .IR libbsd
182 library.
183 .SH BUGS
184 If the destination string of a
185 .BR strcpy ()
186 is not large enough, then anything might happen.
187 Overflowing fixed-length string buffers is a favorite cracker technique
188 for taking complete control of the machine.
189 Any time a program reads or copies data into a buffer,
190 the program first needs to check that there's enough space.
191 This may be unnecessary if you can show that overflow is impossible,
192 but be careful: programs can get changed over time,
193 in ways that may make the impossible possible.
194 .SH SEE ALSO
195 .BR bcopy (3),
196 .BR memccpy (3),
197 .BR memcpy (3),
198 .BR memmove (3),
199 .BR stpcpy (3),
200 .BR stpncpy (3),
201 .BR strdup (3),
202 .BR string (3),
203 .BR wcscpy (3),
204 .BR wcsncpy (3)