Deleted Added
full compact
strdup.3 (165903) strdup.3 (185690)
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.

--- 12 unchanged lines hidden (view full) ---

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
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.

--- 12 unchanged lines hidden (view full) ---

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: head/lib/libc/string/strdup.3 165903 2007-01-09 00:28:16Z imp $
29.\" $FreeBSD: head/lib/libc/string/strdup.3 185690 2008-12-06 09:37:54Z kib $
30.\"
30.\"
31.Dd June 9, 1993
31.Dd Dec 5, 2008
32.Dt STRDUP 3
33.Os
34.Sh NAME
32.Dt STRDUP 3
33.Os
34.Sh NAME
35.Nm strdup
35.Nm strdup ,
36.Nm strndup
36.Nd save a copy of a string
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In string.h
41.Ft char *
42.Fn strdup "const char *str"
37.Nd save a copy of a string
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strdup "const char *str"
44.Ft char *
45.Fn strndup "const char *str" "size_t len"
43.Sh DESCRIPTION
44The
45.Fn strdup
46function
47allocates sufficient memory for a copy
48of the string
49.Fa str ,
50does the copy, and returns a pointer to it.
51The pointer may subsequently be used as an
52argument to the function
53.Xr free 3 .
54.Pp
55If insufficient memory is available, NULL is returned and
56.Va errno
57is set to
58.Er ENOMEM .
46.Sh DESCRIPTION
47The
48.Fn strdup
49function
50allocates sufficient memory for a copy
51of the string
52.Fa str ,
53does the copy, and returns a pointer to it.
54The pointer may subsequently be used as an
55argument to the function
56.Xr free 3 .
57.Pp
58If insufficient memory is available, NULL is returned and
59.Va errno
60is set to
61.Er ENOMEM .
62.Pp
63The
64.Fn strndup
65function copies at most
66.Fa len
67characters from the string
68.Fa str
69always
70.Dv NUL
71terminating the copied string.
59.Sh SEE ALSO
60.Xr free 3 ,
61.Xr malloc 3
62.Sh HISTORY
63The
64.Fn strdup
65function first appeared in
66.Bx 4.4 .
72.Sh SEE ALSO
73.Xr free 3 ,
74.Xr malloc 3
75.Sh HISTORY
76The
77.Fn strdup
78function first appeared in
79.Bx 4.4 .
80The
81.Fn strndup
82function was added in
83.Fx 8.0 .