]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strdup.3
err.3: EXAMPLES: use EXIT_FAILURE rather than 1 as exit status
[thirdparty/man-pages.git] / man3 / strdup.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da
MK
24.\"
25.\" References consulted:
26.\" Linux libc source code
27.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28.\" 386BSD man pages
29.\" Modified Sun Jul 25 10:41:34 1993 by Rik Faith (faith@cs.unc.edu)
30.\" Modified Wed Oct 17 01:12:26 2001 by John Levon <moz@compsoc.man.ac.uk>
9ba01802 31.TH STRDUP 3 2019-03-06 "GNU" "Linux Programmer's Manual"
fea681da
MK
32.SH NAME
33strdup, strndup, strdupa, strndupa \- duplicate a string
34.SH SYNOPSIS
35.nf
36.B #include <string.h>
68e4db0a 37.PP
fea681da 38.BI "char *strdup(const char *" s );
68e4db0a 39.PP
fea681da 40.BI "char *strndup(const char *" s ", size_t " n );
fea681da 41.BI "char *strdupa(const char *" s );
fea681da 42.BI "char *strndupa(const char *" s ", size_t " n );
fea681da 43.fi
68e4db0a 44.PP
cc4615cc
MK
45.in -4n
46Feature Test Macro Requirements for glibc (see
47.BR feature_test_macros (7)):
48.in
68e4db0a 49.PP
8a3a2952 50.PD 0
d5c58392 51.ad l
cc4615cc 52.BR strdup ():
d5c58392 53.RS 4
d59161f9 54_XOPEN_SOURCE\ >=\ 500
cf7fa0a1 55.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
cf7fa0a1 56 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
d59161f9 57 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
d5c58392
MK
58.RE
59.PP
8a3a2952
MK
60.BR strndup ():
61.RS 4
62.TP 4
63Since glibc 2.10:
b0da7b8b 64_POSIX_C_SOURCE\ >=\ 200809L
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
46d8df8e
MK
79is a duplicate of the string
80.IR s .
1c44bd5b 81Memory for the new string is
60a90ecd
MK
82obtained with
83.BR malloc (3),
84and can be freed with
85.BR free (3).
847e0d88 86.PP
60a90ecd
MK
87The
88.BR strndup ()
33a0ccb2 89function is similar, but copies at most
46d8df8e
MK
90.I n
91bytes.
92If
93.I s
94is longer than
95.IR n ,
96only
97.I n
d1a71985 98bytes are copied, and a terminating null byte (\(aq\e0\(aq) is added.
847e0d88 99.PP
60a90ecd
MK
100.BR strdupa ()
101and
102.BR strndupa ()
103are similar, but use
104.BR alloca (3)
c13182ef 105to allocate the buffer.
33a0ccb2 106They are available only when using the GNU
60a90ecd
MK
107GCC suite, and suffer from the same limitations described in
108.BR alloca (3).
47297adb 109.SH RETURN VALUE
6a788310 110On success, the
60a90ecd
MK
111.BR strdup ()
112function returns a pointer to the duplicated
6a788310
MK
113string.
114It returns NULL if insufficient memory was available, with
115.I errno
116set to indicate the cause of the error.
fea681da
MK
117.SH ERRORS
118.TP
119.B ENOMEM
120Insufficient memory available to allocate duplicate string.
7ab7eb48
MS
121.SH ATTRIBUTES
122For an explanation of the terms used in this section, see
123.BR attributes (7).
124.TS
125allbox;
126lbw31 lb lb
127l l l.
128Interface Attribute Value
129T{
130.BR strdup (),
131.BR strndup (),
132.BR strdupa (),
133.br
134.BR strndupa ()
135T} Thread safety MT-Safe
136.TE
847e0d88 137.sp 1
47297adb 138.SH CONFORMING TO
b14d4aa5 139.\" 4.3BSD-Reno, not (first) 4.3BSD.
68e1685c
MK
140.BR strdup ()
141conforms to SVr4, 4.3BSD, POSIX.1-2001.
82f4ea01
MK
142.BR strndup ()
143conforms to POSIX.1-2008.
144.BR strdupa ()
60a90ecd
MK
145and
146.BR strndupa ()
147are GNU extensions.
47297adb 148.SH SEE ALSO
fea681da
MK
149.BR alloca (3),
150.BR calloc (3),
151.BR free (3),
152.BR malloc (3),
1709027c 153.BR realloc (3),
d095200e 154.BR string (3),
cc4615cc 155.BR wcsdup (3)