xref: /freebsd/lib/libc/string/strdup.3 (revision 9316908f481c5d3e6474d31e013f238b931c2972)
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*9316908fSConrad Meyer.Dd June 6, 2018
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.
5458f0484fSRodney W. GrimesThe pointer may subsequently be used as an
5558f0484fSRodney W. Grimesargument to the function
5658f0484fSRodney W. Grimes.Xr free 3 .
5758f0484fSRodney W. Grimes.Pp
58545c943aSMike BarcroftIf insufficient memory is available, NULL is returned and
59545c943aSMike Barcroft.Va errno
60545c943aSMike Barcroftis set to
61545c943aSMike Barcroft.Er ENOMEM .
62f0eca40aSKonstantin Belousov.Pp
63f0eca40aSKonstantin BelousovThe
64f0eca40aSKonstantin Belousov.Fn strndup
65f0eca40aSKonstantin Belousovfunction copies at most
66f0eca40aSKonstantin Belousov.Fa len
67f0eca40aSKonstantin Belousovcharacters from the string
68f0eca40aSKonstantin Belousov.Fa str
69f0eca40aSKonstantin Belousovalways
70f0eca40aSKonstantin Belousov.Dv NUL
71f0eca40aSKonstantin Belousovterminating the copied string.
7258f0484fSRodney W. Grimes.Sh SEE ALSO
7375141cc9SWolfram Schneider.Xr free 3 ,
745174a6a2SEdward Tomasz Napierala.Xr malloc 3 ,
755174a6a2SEdward Tomasz Napierala.Xr wcsdup 3
76*9316908fSConrad Meyer.Sh STANDARDS
77*9316908fSConrad MeyerThe
78*9316908fSConrad Meyer.Fn strdup
79*9316908fSConrad Meyerfunction is specified by
80*9316908fSConrad Meyer.St -p1003.1-2001 .
81*9316908fSConrad MeyerThe
82*9316908fSConrad Meyer.Fn strndup
83*9316908fSConrad Meyerfunction is specified by
84*9316908fSConrad Meyer.St -p1003.1-2008 .
8558f0484fSRodney W. Grimes.Sh HISTORY
8658f0484fSRodney W. GrimesThe
8758f0484fSRodney W. Grimes.Fn strdup
887bdf80e5SMike Pritchardfunction first appeared in
897bdf80e5SMike Pritchard.Bx 4.4 .
90f0eca40aSKonstantin BelousovThe
91f0eca40aSKonstantin Belousov.Fn strndup
92f0eca40aSKonstantin Belousovfunction was added in
932e92f03aSKonstantin Belousov.Fx 7.2 .
94