]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man3/strcpy.3
rename.2: SEE ALSO: add rename(1)
[thirdparty/man-pages.git] / man3 / strcpy.3
index f7b0682b9bd65a68cde5f16df84bd7adf8c8266a..7da5b9796958c56cf1969540c819e39c1a1f9ba4 100644 (file)
@@ -1,5 +1,6 @@
 .\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
 .\"
+.\" %%%LICENSE_START(VERBATIM)
 .\" Permission is granted to make and distribute verbatim copies of this
 .\" manual provided the copyright notice and this permission notice are
 .\" preserved on all copies.
@@ -19,6 +20,7 @@
 .\"
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
 .\"
 .\" References consulted:
 .\"     Linux libc source code
 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
 .\"     Improve discussion of strncpy().
 .\"
-.TH STRCPY 3  2012-07-19 "GNU" "Linux Programmer's Manual"
+.TH STRCPY 3  2019-03-06 "GNU" "Linux Programmer's Manual"
 .SH NAME
 strcpy, strncpy \- copy a string
 .SH SYNOPSIS
 .nf
 .B #include <string.h>
-.sp
+.PP
 .BI "char *strcpy(char *" dest ", const char *" src );
-.sp
+.PP
 .BI "char *strncpy(char *" dest ", const char *" src ", size_t " n );
 .fi
 .SH DESCRIPTION
 The
 .BR strcpy ()
-function copies the string pointed to by \fIsrc\fP,
-including the terminating null byte (\(aq\\0\(aq),
-to the buffer pointed to by \fIdest\fP.
+function copies the string pointed to by
+.IR src ,
+including the terminating null byte (\(aq\e0\(aq),
+to the buffer pointed to by
+.IR dest .
 The strings may not overlap, and the destination string
-\fIdest\fP must be large enough to receive the copy.
+.I dest
+must be large enough to receive the copy.
 .IR "Beware of buffer overruns!"
 (See BUGS.)
 .PP
 The
 .BR strncpy ()
 function is similar, except that at most
-\fIn\fP bytes of \fIsrc\fP are copied.
+.I n
+bytes of
+.I src
+are copied.
 .BR Warning :
 If there is no null byte
-among the first \fIn\fP bytes of \fIsrc\fP,
-the string placed in \fIdest\fP will not be null-terminated.
+among the first
+.I n
+bytes of
+.IR src ,
+the string placed in
+.I dest
+will not be null-terminated.
 .PP
 If the length of
 .I src
@@ -75,22 +88,22 @@ bytes are written.
 A simple implementation of
 .BR strncpy ()
 might be:
+.PP
 .in +4n
-.nf
-
+.EX
 char *
 strncpy(char *dest, const char *src, size_t n)
 {
     size_t i;
 
-    for (i = 0; i < n && src[i] != \(aq\\0\(aq; i++)
+    for (i = 0; i < n && src[i] != \(aq\e0\(aq; i++)
         dest[i] = src[i];
     for ( ; i < n; i++)
-        dest[i] = \(aq\\0\(aq;
+        dest[i] = \(aq\e0\(aq;
 
     return dest;
 }
-.fi
+.EE
 .in
 .SH RETURN VALUE
 The
@@ -98,52 +111,80 @@ The
 and
 .BR strncpy ()
 functions return a pointer to
-the destination string \fIdest\fP.
+the destination string
+.IR dest .
+.SH ATTRIBUTES
+For an explanation of the terms used in this section, see
+.BR attributes (7).
+.TS
+allbox;
+lbw19 lb lb
+l l l.
+Interface      Attribute       Value
+T{
+.BR strcpy (),
+.BR strncpy ()
+T}     Thread safety   MT-Safe
+.TE
 .SH CONFORMING TO
-SVr4, 4.3BSD, C89, C99.
+POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
 .SH NOTES
 Some programmers consider
 .BR strncpy ()
 to be inefficient and error prone.
 If the programmer knows (i.e., includes code to test!)
-that the size of \fIdest\fP is greater than
-the length of \fIsrc\fP, then
+that the size of
+.I dest
+is greater than
+the length of
+.IR src ,
+then
 .BR strcpy ()
 can be used.
-
+.PP
 One valid (and intended) use of
 .BR strncpy ()
 is to copy a C string to a fixed-length buffer
 while ensuring both that the buffer is not overflowed
-and that unused bytes in the target buffer are zeroed out
+and that unused bytes in the destination buffer are zeroed out
 (perhaps to prevent information leaks if the buffer is to be
 written to media or transmitted to another process via an
 interprocess communication technique).
-
-If there is no terminating null byte in the first \fIn\fP
-bytes of \fIsrc\fP,
+.PP
+If there is no terminating null byte in the first
+.I n
+bytes of
+.IR src ,
 .BR strncpy ()
-produces an unterminated string in \fIdest\fP.
-You can force termination using something like the following:
+produces an unterminated string in
+.IR dest .
+If
+.I buf
+has length
+.IR buflen ,
+you can force termination using something like the following:
+.PP
 .in +4n
-.nf
-
-strncpy(buf, str, n);
-if (n > 0)
-    buf[n \- 1]= \(aq\\0\(aq;
-.fi
+.EX
+if (buflen > 0) {
+    strncpy(buf, str, buflen \- 1);
+    buf[buflen \- 1]= \(aq\e0\(aq;
+}
+.EE
 .in
 .PP
-(Of course, the above technique ignores the fact that
-information contained in
+(Of course, the above technique ignores the fact that, if
 .I src
-is lost in the copying to
+contains more than
+.I "buflen\ \-\ 1"
+bytes, information is lost in the copying to
 .IR dest .)
-
+.\"
+.SS strlcpy()
 Some systems (the BSDs, Solaris, and others) provide the following function:
-
+.PP
     size_t strlcpy(char *dest, const char *src, size_t size);
-
+.PP
 .\" http://static.usenix.org/event/usenix99/full_papers/millert/millert_html/index.html
 .\"     "strlcpy and strlcat - consistent, safe, string copy and concatenation"
 .\"     1999 USENIX Annual Technical Conference
@@ -154,7 +195,7 @@ but it copies at most
 bytes to
 .IR dest ,
 always adds a terminating null byte,
-and does not pad the target with (further) null bytes.
+and does not pad the destination with (further) null bytes.
 This function fixes some of the problems of
 .BR strcpy ()
 and