1*ed82d07cSmmcc.\" $OpenBSD: strdup.3,v 1.22 2015/12/01 01:32:48 mmcc Exp $ 2b76173f7Smillert.\" 3b76173f7Smillert.\" Copyright (c) 1990, 1991, 1993 4b76173f7Smillert.\" The Regents of the University of California. All rights reserved. 5df930be7Sderaadt.\" 6df930be7Sderaadt.\" Redistribution and use in source and binary forms, with or without 7df930be7Sderaadt.\" modification, are permitted provided that the following conditions 8df930be7Sderaadt.\" are met: 9df930be7Sderaadt.\" 1. Redistributions of source code must retain the above copyright 10df930be7Sderaadt.\" notice, this list of conditions and the following disclaimer. 11df930be7Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright 12df930be7Sderaadt.\" notice, this list of conditions and the following disclaimer in the 13df930be7Sderaadt.\" documentation and/or other materials provided with the distribution. 146580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors 15df930be7Sderaadt.\" may be used to endorse or promote products derived from this software 16df930be7Sderaadt.\" without specific prior written permission. 17df930be7Sderaadt.\" 18df930be7Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19df930be7Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20df930be7Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21df930be7Sderaadt.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22df930be7Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23df930be7Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24df930be7Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25df930be7Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26df930be7Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27df930be7Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28df930be7Sderaadt.\" SUCH DAMAGE. 29df930be7Sderaadt.\" 30b76173f7Smillert.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 31df930be7Sderaadt.\" 32*ed82d07cSmmcc.Dd $Mdocdate: December 1 2015 $ 33df930be7Sderaadt.Dt STRDUP 3 34df930be7Sderaadt.Os 35df930be7Sderaadt.Sh NAME 36243f3935Stedu.Nm strdup , 37243f3935Stedu.Nm strndup 38df930be7Sderaadt.Nd save a copy of a string 39df930be7Sderaadt.Sh SYNOPSIS 4064d4e987Stedu.In string.h 41df930be7Sderaadt.Ft char * 42828c4884Saaron.Fn strdup "const char *s" 43243f3935Stedu.Ft char * 44243f3935Stedu.Fn strndup "const char *s" "size_t maxlen" 45df930be7Sderaadt.Sh DESCRIPTION 46df930be7SderaadtThe 47df930be7Sderaadt.Fn strdup 4860ab2826Saaronfunction allocates sufficient memory for a copy of the string 49828c4884Saaron.Fa s , 50df930be7Sderaadtdoes the copy, and returns a pointer to it. 5160ab2826SaaronThe pointer may subsequently be used as an argument to the function 52df930be7Sderaadt.Xr free 3 . 53b76173f7Smillert.Pp 54243f3935SteduThe 55243f3935Stedu.Fn strndup 56243f3935Stedufunction behaves similarly to 57243f3935Stedu.Nm strdup 58243f3935Stedubut only copies up to 59243f3935Stedu.Fa maxlen 60243f3935Steducharacters from 61243f3935Stedu.Fa s . 62243f3935SteduThe resulting string is always NUL-terminated. 63243f3935Stedu.Pp 64*ed82d07cSmmccIf the memory allocation fails, 65828c4884Saaron.Dv NULL 66828c4884Saaronis returned. 67828c4884Saaron.Sh EXAMPLES 68828c4884SaaronThe following will point 69828c4884Saaron.Va p 701e5ede29Scloderto an allocated area of memory containing the NUL-terminated string 71828c4884Saaron.Qq foobar : 72828c4884Saaron.Bd -literal -offset indent 73828c4884Saaronchar *p; 74828c4884Saaron 750953621eSkettenisp = strdup("foobar"); 768c40de08Sschwarzeif (p == NULL) 778c40de08Sschwarze err(1, NULL); 78828c4884Saaron.Ed 79a47342eeSaaron.Sh ERRORS 80a47342eeSaaronThe 81a47342eeSaaron.Fn strdup 82985073d4Slumand 83985073d4Slum.Fn strndup 84985073d4Slumfunctions may fail and set the external variable 85a47342eeSaaron.Va errno 86a47342eeSaaronfor any of the errors specified for the library function 87a47342eeSaaron.Xr malloc 3 . 88df930be7Sderaadt.Sh SEE ALSO 89d11e4704Spjanzen.Xr free 3 , 90d11e4704Spjanzen.Xr malloc 3 , 91d11e4704Spjanzen.Xr strcpy 3 , 92f861ae8dSderaadt.Xr strlcpy 3 , 938c40de08Sschwarze.Xr strlen 3 , 948c40de08Sschwarze.Xr wcsdup 3 95243f3935Stedu.Sh STANDARDS 96243f3935SteduThe 97243f3935Stedu.Fn strdup 98243f3935Steduand 99243f3935Stedu.Fn strndup 100243f3935Stedufunctions conform to 101243f3935Stedu.St -p1003.1-2008 . 1028067f1adSjmc.Sh HISTORY 103cf828729SschwarzeA 1048067f1adSjmc.Fn strdup 105cf828729Sschwarzemacro was first used in the 106cf828729Sschwarze.Bx 4.1c 107cf828729Sschwarzedebugger, 108cf828729Sschwarze.Sy dbx . 109cf828729SschwarzeIt was rewritten as a C function for the 110cf828729Sschwarze.Bx 4.3 111cf828729Sschwarze.Xr inetd 8 112cf828729Sschwarzeand first appeared in the C library of 1138c40de08Sschwarze.Bx 4.3 Reno . 1148067f1adSjmcThe 1158067f1adSjmc.Fn strndup 1168c40de08Sschwarzefunction appeared in glibc 2.0, was reimplemented for 1178c40de08Sschwarze.Nx 4.0 , 1188c40de08Sschwarzeand ported to 1198067f1adSjmc.Ox 4.8 . 120