xref: /freebsd/lib/libc/string/strdup.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
123fb3b97cSEd Maste.\" 3. Neither the name of the University nor the names of its contributors
1358f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1458f0484fSRodney W. Grimes.\"    without specific prior written permission.
1558f0484fSRodney W. Grimes.\"
1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2658f0484fSRodney W. Grimes.\" SUCH DAMAGE.
2758f0484fSRodney W. Grimes.\"
28b34f8412SConrad Meyer.Dd May 5, 2020
2958f0484fSRodney W. Grimes.Dt STRDUP 3
3058f0484fSRodney W. Grimes.Os
3158f0484fSRodney W. Grimes.Sh NAME
32f0eca40aSKonstantin Belousov.Nm strdup ,
33f0eca40aSKonstantin Belousov.Nm strndup
3458f0484fSRodney W. Grimes.Nd save a copy of a string
3525bb73e0SAlexey Zelkin.Sh LIBRARY
3625bb73e0SAlexey Zelkin.Lb libc
3758f0484fSRodney W. Grimes.Sh SYNOPSIS
3832eef9aeSRuslan Ermilov.In string.h
3958f0484fSRodney W. Grimes.Ft char *
4058f0484fSRodney W. Grimes.Fn strdup "const char *str"
41f0eca40aSKonstantin Belousov.Ft char *
42f0eca40aSKonstantin Belousov.Fn strndup "const char *str" "size_t len"
4358f0484fSRodney W. Grimes.Sh DESCRIPTION
4458f0484fSRodney W. GrimesThe
4558f0484fSRodney W. Grimes.Fn strdup
4658f0484fSRodney W. Grimesfunction
4758f0484fSRodney W. Grimesallocates sufficient memory for a copy
4858f0484fSRodney W. Grimesof the string
4958f0484fSRodney W. Grimes.Fa str ,
5058f0484fSRodney W. Grimesdoes the copy, and returns a pointer to it.
51b34f8412SConrad MeyerThe memory is allocated with
52b34f8412SConrad Meyer.Xr malloc 3
53b34f8412SConrad Meyerand should be released with
54b34f8412SConrad Meyer.Xr free 3
55b34f8412SConrad Meyerwhen no longer needed.
56f0eca40aSKonstantin Belousov.Pp
57f0eca40aSKonstantin BelousovThe
58f0eca40aSKonstantin Belousov.Fn strndup
59f0eca40aSKonstantin Belousovfunction copies at most
60f0eca40aSKonstantin Belousov.Fa len
61f0eca40aSKonstantin Belousovcharacters from the string
62f0eca40aSKonstantin Belousov.Fa str
63f0eca40aSKonstantin Belousovalways
64f0eca40aSKonstantin Belousov.Dv NUL
65f0eca40aSKonstantin Belousovterminating the copied string.
66b34f8412SConrad Meyer.Sh RETURN VALUES
67b34f8412SConrad MeyerIf insufficient memory is available, NULL is returned and
68b34f8412SConrad Meyer.Va errno
69b34f8412SConrad Meyeris set to
70b34f8412SConrad Meyer.Er ENOMEM .
71b34f8412SConrad MeyerOtherwise, the
72b34f8412SConrad Meyer.Fn strdup
73b34f8412SConrad Meyerfamily of functions return a pointer to the copied string.
7458f0484fSRodney W. Grimes.Sh SEE ALSO
7575141cc9SWolfram Schneider.Xr free 3 ,
765174a6a2SEdward Tomasz Napierala.Xr malloc 3 ,
775174a6a2SEdward Tomasz Napierala.Xr wcsdup 3
789316908fSConrad Meyer.Sh STANDARDS
799316908fSConrad MeyerThe
809316908fSConrad Meyer.Fn strdup
819316908fSConrad Meyerfunction is specified by
829316908fSConrad Meyer.St -p1003.1-2001 .
839316908fSConrad MeyerThe
849316908fSConrad Meyer.Fn strndup
859316908fSConrad Meyerfunction is specified by
869316908fSConrad Meyer.St -p1003.1-2008 .
8758f0484fSRodney W. Grimes.Sh HISTORY
8858f0484fSRodney W. GrimesThe
8958f0484fSRodney W. Grimes.Fn strdup
907bdf80e5SMike Pritchardfunction first appeared in
91*87973534SJessica Clarke.Bx 4.3 Reno .
92f0eca40aSKonstantin BelousovThe
93f0eca40aSKonstantin Belousov.Fn strndup
94f0eca40aSKonstantin Belousovfunction was added in
952e92f03aSKonstantin Belousov.Fx 7.2 .
96