summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/strdup.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/strdup.3')
-rw-r--r--src/lib/libc/string/strdup.389
1 files changed, 67 insertions, 22 deletions
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3
index 925cbf3d46..02d5666a9c 100644
--- a/src/lib/libc/string/strdup.3
+++ b/src/lib/libc/string/strdup.3
@@ -1,5 +1,7 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" $OpenBSD: strdup.3,v 1.18 2011/07/25 00:38:53 schwarze Exp $
2.\" All rights reserved. 2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
3.\" 5.\"
4.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
@@ -9,11 +11,7 @@
9.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the 12.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution. 13.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software 14.\" 3. Neither the name of the University nor the names of its contributors
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software 15.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission. 16.\" without specific prior written permission.
19.\" 17.\"
@@ -29,37 +27,84 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 28.\" SUCH DAMAGE.
31.\" 29.\"
32.\" from: @(#)strdup.3 5.3 (Berkeley) 4/19/91 30.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
33.\" $Id: strdup.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
34.\" 31.\"
35.Dd April 19, 1991 32.Dd $Mdocdate: July 25 2011 $
36.Dt STRDUP 3 33.Dt STRDUP 3
37.Os 34.Os
38.Sh NAME 35.Sh NAME
39.Nm strdup 36.Nm strdup ,
37.Nm strndup
40.Nd save a copy of a string 38.Nd save a copy of a string
41.Sh SYNOPSIS 39.Sh SYNOPSIS
42.Fd #include <string.h> 40.Fd #include <string.h>
43.Ft char * 41.Ft char *
44.Fn strdup "const char *str" 42.Fn strdup "const char *s"
43.Ft char *
44.Fn strndup "const char *s" "size_t maxlen"
45.Sh DESCRIPTION 45.Sh DESCRIPTION
46The 46The
47.Fn strdup 47.Fn strdup
48function 48function allocates sufficient memory for a copy of the string
49allocates sufficient memory for a copy 49.Fa s ,
50of the string
51.Fa str ,
52does the copy, and returns a pointer to it. 50does the copy, and returns a pointer to it.
53The pointer may subsequently be used as an 51The pointer may subsequently be used as an argument to the function
54argument to the function
55.Xr free 3 . 52.Xr free 3 .
53.Pp
54The
55.Fn strndup
56function behaves similarly to
57.Nm strdup
58but only copies up to
59.Fa maxlen
60characters from
61.Fa s .
62The resulting string is always NUL-terminated.
63.Pp
64If insufficient memory is available,
65.Dv NULL
66is returned.
67.Sh EXAMPLES
68The following will point
69.Va p
70to an allocated area of memory containing the NUL-terminated string
71.Qq foobar :
72.Bd -literal -offset indent
73char *p;
74
75p = strdup("foobar");
76if (p == NULL)
77 err(1, NULL);
78.Ed
79.Sh ERRORS
80The
81.Fn strdup
82function may fail and set the external variable
83.Va errno
84for any of the errors specified for the library function
85.Xr malloc 3 .
56.Sh SEE ALSO 86.Sh SEE ALSO
57.Xr free 3 , 87.Xr free 3 ,
58.Xr malloc 3 , 88.Xr malloc 3 ,
59.Xt strcpy 3 , 89.Xr strcpy 3 ,
60.Xt strlen 3 90.Xr strlcpy 3 ,
91.Xr strlen 3 ,
92.Xr wcsdup 3
93.Sh STANDARDS
94The
95.Fn strdup
96and
97.Fn strndup
98functions conform to
99.St -p1003.1-2008 .
61.Sh HISTORY 100.Sh HISTORY
62The 101The
63.Fn strdup 102.Fn strdup
64function 103function first appeared in
65.Ud . 104.Bx 4.3 Reno .
105The
106.Fn strndup
107function appeared in glibc 2.0, was reimplemented for
108.Nx 4.0 ,
109and ported to
110.Ox 4.8 .