xref: /freebsd/lib/libc/string/strdup.3 (revision e2afbc45258f2fa4bdcf126e959ac660e76fc802)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd May 5, 2020
29.Dt STRDUP 3
30.Os
31.Sh NAME
32.Nm strdup ,
33.Nm strdupa ,
34.Nm strndup ,
35.Nm strndupa
36.Nd save a copy of a string
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In string.h
41.Ft char *
42.Fn strdup "const char *str"
43.Ft char *
44.Fn strdupa "const char *str"
45.Ft char *
46.Fn strndup "const char *str" "size_t len"
47.Ft char *
48.Fn strndupa "const char *str" "size_t len"
49.Sh DESCRIPTION
50The
51.Fn strdup
52function
53allocates sufficient memory for a copy
54of the string
55.Fa str ,
56does the copy, and returns a pointer to it.
57The memory is allocated with
58.Xr malloc 3
59and should be released with
60.Xr free 3
61when no longer needed.
62.Pp
63The
64.Fn strndup
65function copies at most
66.Fa len
67characters from the string
68.Fa str
69always
70.Dv NUL
71terminating the copied string.
72.Pp
73The
74.Fn strdupa
75function is identical to
76.Fn strdup
77but allocates the memory with
78.Xr alloca 3 .
79Similarly, the
80.Fn strndupa
81function is identical to
82.Fn strndup ,
83but allocates the memory with
84.Xr alloca 3 .
85.Sh RETURN VALUES
86If insufficient memory is available, NULL is returned and
87.Va errno
88is set to
89.Er ENOMEM .
90Otherwise, the
91.Fn strdup
92family of functions return a pointer to the copied string.
93.Sh SEE ALSO
94.Xr alloca 3 ,
95.Xr free 3 ,
96.Xr malloc 3 ,
97.Xr wcsdup 3
98.Sh STANDARDS
99The
100.Fn strdup
101function is specified by
102.St -p1003.1-2001 .
103The
104.Fn strndup
105function is specified by
106.St -p1003.1-2008 .
107The
108.Fn strdupa
109and
110.Fn strndupa
111functions are extensions,
112taken from glibc.
113.Sh HISTORY
114The
115.Fn strdup
116function first appeared in
117.Bx 4.3 Reno .
118The
119.Fn strndup
120function was added in
121.Fx 7.2 .
122The
123.Fn strdupa
124and
125.Fn strndupa
126functions were added in
127.Fx 15.1 .
128