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.\" 4. 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.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 29.\" $FreeBSD: src/lib/libc/string/strdup.3,v 1.13 2008/12/09 09:24:25 maxim Exp $ 30.\" $DragonFly: src/lib/libc/string/strdup.3,v 1.5 2004/12/26 12:37:08 swildner Exp $ 31.\" 32.Dd December 5, 2008 33.Dt STRDUP 3 34.Os 35.Sh NAME 36.Nm strdup , 37.Nm strndup 38.Nd save a copy of a string 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In string.h 43.Ft char * 44.Fn strdup "const char *str" 45.Ft char * 46.Fn strndup "const char *str" "size_t len" 47.Sh DESCRIPTION 48The 49.Fn strdup 50function 51allocates sufficient memory for a copy 52of the string 53.Fa str , 54does the copy, and returns a pointer to it. 55The pointer may subsequently be used as an 56argument to the function 57.Xr free 3 . 58.Pp 59If insufficient memory is available, NULL is returned and 60.Va errno 61is set to 62.Er ENOMEM . 63.Pp 64The 65.Fn strndup 66function copies at most 67.Fa len 68characters from the string 69.Fa str 70always 71.Dv NUL 72terminating the copied string. 73.Sh ERRORS 74The 75.Nm 76function may fail if: 77.Bl -tag -width Er 78.It Bq Er ENOMEM 79No memory could be allocated for the copy of the string. 80.El 81.Sh SEE ALSO 82.Xr free 3 , 83.Xr malloc 3 84.Sh STANDARDS 85The 86.Nm 87function conforms to 88.St -p1003.1-2004 . 89.Sh HISTORY 90The 91.Fn strdup 92function first appeared in 93.Bx 4.4 . 94The 95.Fn strndup 96function was added in 97.Fx 8.0 . 98