OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / 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  2009-12-04 "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     size_t i;
80
81     for (i = 0 ; i < n && src[i] != \(aq\\0\(aq ; i++)
82         dest[i] = src[i];
83     for ( ; i < n ; i++)
84         dest[i] = \(aq\\0\(aq;
85
86     return dest;
87 }
88 .fi
89 .in
90 .SH "RETURN VALUE"
91 The
92 .BR strcpy ()
93 and
94 .BR strncpy ()
95 functions return a pointer to
96 the destination string \fIdest\fP.
97 .SH "CONFORMING TO"
98 SVr4, 4.3BSD, C89, C99.
99 .SH NOTES
100 Some programmers consider
101 .BR strncpy ()
102 to be inefficient and error prone.
103 If the programmer knows (i.e., includes code to test!)
104 that the size of \fIdest\fP is greater than
105 the length of \fIsrc\fP, then
106 .BR strcpy ()
107 can be used.
108
109 If there is no terminating null byte in the first \fIn\fP
110 characters of \fIsrc\fP,
111 .BR strncpy ()
112 produces an unterminated string in \fIdest\fP.
113 Programmers often prevent this mistake by forcing termination
114 as follows:
115 .in +4n
116 .nf
117
118 strncpy(buf, str, n);
119 if (n > 0)
120     buf[n \- 1]= \(aq\\0\(aq;
121 .fi
122 .in
123 .SH BUGS
124 If the destination string of a
125 .BR strcpy ()
126 is not large enough, then anything might happen.
127 Overflowing fixed-length string buffers is a favorite cracker technique
128 for taking complete control of the machine.
129 Any time a program reads or copies data into a buffer,
130 the program first needs to check that there's enough space.
131 This may be unnecessary if you can show that overflow is impossible,
132 but be careful: programs can get changed over time,
133 in ways that may make the impossible possible.
134 .SH "SEE ALSO"
135 .BR bcopy (3),
136 .BR memccpy (3),
137 .BR memcpy (3),
138 .BR memmove (3),
139 .BR stpcpy (3),
140 .BR strdup (3),
141 .BR wcscpy (3),
142 .BR wcsncpy (3)