OSDN Git Service

(split) LDP_man-pages: update original to v3.35.
[linuxjm/LDP_man-pages.git] / original / man3 / strcpy.3
1 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\" Modified Sat Jul 24 18:06:49 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Modified Fri Aug 25 23:17:51 1995 by Andries Brouwer (aeb@cwi.nl)
29 .\" Modified Wed Dec 18 00:47:18 1996 by Andries Brouwer (aeb@cwi.nl)
30 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
31 .\"     Improve discussion of strncpy().
32 .\"
33 .TH STRCPY 3  2010-09-20 "GNU" "Linux Programmer's Manual"
34 .SH NAME
35 strcpy, strncpy \- copy a string
36 .SH SYNOPSIS
37 .nf
38 .B #include <string.h>
39 .sp
40 .BI "char *strcpy(char *" dest ", const char *" src );
41 .sp
42 .BI "char *strncpy(char *" dest ", const char *" src ", size_t " n );
43 .fi
44 .SH DESCRIPTION
45 The
46 .BR strcpy ()
47 function copies the string pointed to by \fIsrc\fP,
48 including the terminating null byte (\(aq\\0\(aq),
49 to the buffer pointed to by \fIdest\fP.
50 The strings may not overlap, and the destination string
51 \fIdest\fP must be large enough to receive the copy.
52 .PP
53 The
54 .BR strncpy ()
55 function is similar, except that at most
56 \fIn\fP bytes of \fIsrc\fP are copied.
57 .BR Warning :
58 If there is no null byte
59 among the first \fIn\fP bytes of \fIsrc\fP,
60 the string placed in \fIdest\fP will not be null-terminated.
61 .PP
62 If the length of
63 .I src
64 is less than
65 .IR n ,
66 .BR strncpy ()
67 pads the remainder of
68 .I dest
69 with null bytes.
70 .PP
71 A simple implementation of
72 .BR strncpy ()
73 might be:
74 .in +4n
75 .nf
76
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\\0\(aq; i++)
83         dest[i] = src[i];
84     for ( ; i < n; i++)
85         dest[i] = \(aq\\0\(aq;
86
87     return dest;
88 }
89 .fi
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 \fIdest\fP.
98 .SH "CONFORMING TO"
99 SVr4, 4.3BSD, C89, C99.
100 .SH NOTES
101 Some programmers consider
102 .BR strncpy ()
103 to be inefficient and error prone.
104 If the programmer knows (i.e., includes code to test!)
105 that the size of \fIdest\fP is greater than
106 the length of \fIsrc\fP, then
107 .BR strcpy ()
108 can be used.
109
110 If there is no terminating null byte in the first \fIn\fP
111 characters of \fIsrc\fP,
112 .BR strncpy ()
113 produces an unterminated string in \fIdest\fP.
114 Programmers often prevent this mistake by forcing termination
115 as follows:
116 .in +4n
117 .nf
118
119 strncpy(buf, str, n);
120 if (n > 0)
121     buf[n \- 1]= \(aq\\0\(aq;
122 .fi
123 .in
124 .SH BUGS
125 If the destination string of a
126 .BR strcpy ()
127 is not large enough, then anything might happen.
128 Overflowing fixed-length string buffers is a favorite cracker technique
129 for taking complete control of the machine.
130 Any time a program reads or copies data into a buffer,
131 the program first needs to check that there's enough space.
132 This may be unnecessary if you can show that overflow is impossible,
133 but be careful: programs can get changed over time,
134 in ways that may make the impossible possible.
135 .SH "SEE ALSO"
136 .BR bcopy (3),
137 .BR memccpy (3),
138 .BR memcpy (3),
139 .BR memmove (3),
140 .BR stpcpy (3),
141 .BR stpncpy (3),
142 .BR strdup (3),
143 .BR string (3),
144 .BR wcscpy (3),
145 .BR wcsncpy (3)