xref: /freebsd/lib/libc/string/strdup.3 (revision b34f8412c503f620d86e8a2d47cf70af515aad78)
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.\"
2858f0484fSRodney W. Grimes.\"     @(#)strdup.3	8.1 (Berkeley) 6/9/93
297f3dea24SPeter Wemm.\" $FreeBSD$
3058f0484fSRodney W. Grimes.\"
31*b34f8412SConrad Meyer.Dd May 5, 2020
3258f0484fSRodney W. Grimes.Dt STRDUP 3
3358f0484fSRodney W. Grimes.Os
3458f0484fSRodney W. Grimes.Sh NAME
35f0eca40aSKonstantin Belousov.Nm strdup ,
36f0eca40aSKonstantin Belousov.Nm strndup
3758f0484fSRodney W. Grimes.Nd save a copy of a string
3825bb73e0SAlexey Zelkin.Sh LIBRARY
3925bb73e0SAlexey Zelkin.Lb libc
4058f0484fSRodney W. Grimes.Sh SYNOPSIS
4132eef9aeSRuslan Ermilov.In string.h
4258f0484fSRodney W. Grimes.Ft char *
4358f0484fSRodney W. Grimes.Fn strdup "const char *str"
44f0eca40aSKonstantin Belousov.Ft char *
45f0eca40aSKonstantin Belousov.Fn strndup "const char *str" "size_t len"
4658f0484fSRodney W. Grimes.Sh DESCRIPTION
4758f0484fSRodney W. GrimesThe
4858f0484fSRodney W. Grimes.Fn strdup
4958f0484fSRodney W. Grimesfunction
5058f0484fSRodney W. Grimesallocates sufficient memory for a copy
5158f0484fSRodney W. Grimesof the string
5258f0484fSRodney W. Grimes.Fa str ,
5358f0484fSRodney W. Grimesdoes the copy, and returns a pointer to it.
54*b34f8412SConrad MeyerThe memory is allocated with
55*b34f8412SConrad Meyer.Xr malloc 3
56*b34f8412SConrad Meyerand should be released with
57*b34f8412SConrad Meyer.Xr free 3
58*b34f8412SConrad Meyerwhen no longer needed.
59f0eca40aSKonstantin Belousov.Pp
60f0eca40aSKonstantin BelousovThe
61f0eca40aSKonstantin Belousov.Fn strndup
62f0eca40aSKonstantin Belousovfunction copies at most
63f0eca40aSKonstantin Belousov.Fa len
64f0eca40aSKonstantin Belousovcharacters from the string
65f0eca40aSKonstantin Belousov.Fa str
66f0eca40aSKonstantin Belousovalways
67f0eca40aSKonstantin Belousov.Dv NUL
68f0eca40aSKonstantin Belousovterminating the copied string.
69*b34f8412SConrad Meyer.Sh RETURN VALUES
70*b34f8412SConrad MeyerIf insufficient memory is available, NULL is returned and
71*b34f8412SConrad Meyer.Va errno
72*b34f8412SConrad Meyeris set to
73*b34f8412SConrad Meyer.Er ENOMEM .
74*b34f8412SConrad MeyerOtherwise, the
75*b34f8412SConrad Meyer.Fn strdup
76*b34f8412SConrad Meyerfamily of functions return a pointer to the copied string.
7758f0484fSRodney W. Grimes.Sh SEE ALSO
7875141cc9SWolfram Schneider.Xr free 3 ,
795174a6a2SEdward Tomasz Napierala.Xr malloc 3 ,
805174a6a2SEdward Tomasz Napierala.Xr wcsdup 3
819316908fSConrad Meyer.Sh STANDARDS
829316908fSConrad MeyerThe
839316908fSConrad Meyer.Fn strdup
849316908fSConrad Meyerfunction is specified by
859316908fSConrad Meyer.St -p1003.1-2001 .
869316908fSConrad MeyerThe
879316908fSConrad Meyer.Fn strndup
889316908fSConrad Meyerfunction is specified by
899316908fSConrad Meyer.St -p1003.1-2008 .
9058f0484fSRodney W. Grimes.Sh HISTORY
9158f0484fSRodney W. GrimesThe
9258f0484fSRodney W. Grimes.Fn strdup
937bdf80e5SMike Pritchardfunction first appeared in
947bdf80e5SMike Pritchard.Bx 4.4 .
95f0eca40aSKonstantin BelousovThe
96f0eca40aSKonstantin Belousov.Fn strndup
97f0eca40aSKonstantin Belousovfunction was added in
982e92f03aSKonstantin Belousov.Fx 7.2 .
99