]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strdup.3
chdir.2, chmod.2, chown.2, gethostname.2, getsid.2, pread.2, setpgid.2, sigaltstack...
[thirdparty/man-pages.git] / man3 / strdup.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
c13182ef 11.\"
fea681da
MK
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
c13182ef 19.\"
fea681da
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" References consulted:
24.\" Linux libc source code
25.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26.\" 386BSD man pages
27.\" Modified Sun Jul 25 10:41:34 1993 by Rik Faith (faith@cs.unc.edu)
28.\" Modified Wed Oct 17 01:12:26 2001 by John Levon <moz@compsoc.man.ac.uk>
8a3a2952 29.TH STRDUP 3 2010-09-15 "GNU" "Linux Programmer's Manual"
fea681da
MK
30.SH NAME
31strdup, strndup, strdupa, strndupa \- duplicate a string
32.SH SYNOPSIS
33.nf
34.B #include <string.h>
35.sp
36.BI "char *strdup(const char *" s );
37.sp
fea681da
MK
38.BI "char *strndup(const char *" s ", size_t " n );
39.br
40.BI "char *strdupa(const char *" s );
41.br
42.BI "char *strndupa(const char *" s ", size_t " n );
fea681da 43.fi
cc4615cc
MK
44.sp
45.in -4n
46Feature Test Macro Requirements for glibc (see
47.BR feature_test_macros (7)):
48.in
8a3a2952 49.PD 0
d5c58392 50.ad l
cc4615cc
MK
51.sp
52.BR strdup ():
d5c58392 53.RS 4
d5c58392
MK
54_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
55_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
3ba63d80
MK
56.br
57|| /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
d5c58392
MK
58.RE
59.PP
8a3a2952
MK
60.BR strndup ():
61.RS 4
62.TP 4
63Since glibc 2.10:
98dbe7af 64POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700
8a3a2952
MK
65.TP
66Before glibc 2.10:
67_GNU_SOURCE
68.RE
69.PP
cc4615cc
MK
70.BR strdupa (),
71.BR strndupa ():
72_GNU_SOURCE
d5c58392 73.ad
8a3a2952 74.PD
fea681da 75.SH DESCRIPTION
60a90ecd
MK
76The
77.BR strdup ()
78function returns a pointer to a new string which
1c44bd5b
MK
79is a duplicate of the string \fIs\fP.
80Memory for the new string is
60a90ecd
MK
81obtained with
82.BR malloc (3),
83and can be freed with
84.BR free (3).
fea681da 85
60a90ecd
MK
86The
87.BR strndup ()
88function is similar, but only copies at most
c13182ef
MK
89\fIn\fP characters.
90If \fIs\fP is longer than \fIn\fP, only \fIn\fP
f81fb444 91characters are copied, and a terminating null byte (\(aq\\0\(aq) is added.
fea681da 92
60a90ecd
MK
93.BR strdupa ()
94and
95.BR strndupa ()
96are similar, but use
97.BR alloca (3)
c13182ef
MK
98to allocate the buffer.
99They are only available when using the GNU
60a90ecd
MK
100GCC suite, and suffer from the same limitations described in
101.BR alloca (3).
fea681da 102.SH "RETURN VALUE"
60a90ecd
MK
103The
104.BR strdup ()
105function returns a pointer to the duplicated
fea681da
MK
106string, or NULL if insufficient memory was available.
107.SH ERRORS
108.TP
109.B ENOMEM
110Insufficient memory available to allocate duplicate string.
111.SH "CONFORMING TO"
b14d4aa5 112.\" 4.3BSD-Reno, not (first) 4.3BSD.
68e1685c
MK
113.BR strdup ()
114conforms to SVr4, 4.3BSD, POSIX.1-2001.
60a90ecd
MK
115.BR strndup (),
116.BR strdupa (),
117and
118.BR strndupa ()
119are GNU extensions.
fea681da
MK
120.SH "SEE ALSO"
121.BR alloca (3),
122.BR calloc (3),
123.BR free (3),
124.BR malloc (3),
1709027c 125.BR realloc (3),
d095200e 126.BR string (3),
cc4615cc 127.BR wcsdup (3)