sigaction.2: Minor clean-ups to Peter Collingbourne's patch
[man-pages.git] / man3 / strdup.3
blob1e1ac34ded99bf4fb3b048455f23647cb60ee34f
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
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.
12 .\"
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.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
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>
31 .TH STRDUP 3  2021-03-22 "GNU" "Linux Programmer's Manual"
32 .SH NAME
33 strdup, strndup, strdupa, strndupa \- duplicate a string
34 .SH SYNOPSIS
35 .nf
36 .B #include <string.h>
37 .PP
38 .BI "char *strdup(const char *" s );
39 .PP
40 .BI "char *strndup(const char *" s ", size_t " n );
41 .BI "char *strdupa(const char *" s );
42 .BI "char *strndupa(const char *" s ", size_t " n );
43 .fi
44 .PP
45 .RS -4
46 Feature Test Macro Requirements for glibc (see
47 .BR feature_test_macros (7)):
48 .RE
49 .PP
50 .BR strdup ():
51 .nf
52     _XOPEN_SOURCE >= 500
53 .\"    || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
54         || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
55         || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
56 .fi
57 .PP
58 .BR strndup ():
59 .nf
60     Since glibc 2.10:
61         _POSIX_C_SOURCE >= 200809L
62     Before glibc 2.10:
63         _GNU_SOURCE
64 .fi
65 .PP
66 .BR strdupa (),
67 .BR strndupa ():
68 .nf
69     _GNU_SOURCE
70 .fi
71 .SH DESCRIPTION
72 The
73 .BR strdup ()
74 function returns a pointer to a new string which
75 is a duplicate of the string
76 .IR s .
77 Memory for the new string is
78 obtained with
79 .BR malloc (3),
80 and can be freed with
81 .BR free (3).
82 .PP
83 The
84 .BR strndup ()
85 function is similar, but copies at most
86 .I n
87 bytes.
89 .I s
90 is longer than
91 .IR n ,
92 only
93 .I n
94 bytes are copied, and a terminating null byte (\(aq\e0\(aq) is added.
95 .PP
96 .BR strdupa ()
97 and
98 .BR strndupa ()
99 are similar, but use
100 .BR alloca (3)
101 to allocate the buffer.
102 They are available only when using the GNU
103 GCC suite, and suffer from the same limitations described in
104 .BR alloca (3).
105 .SH RETURN VALUE
106 On success, the
107 .BR strdup ()
108 function returns a pointer to the duplicated
109 string.
110 It returns NULL if insufficient memory was available, with
111 .I errno
112 set to indicate the error.
113 .SH ERRORS
115 .B ENOMEM
116 Insufficient memory available to allocate duplicate string.
117 .SH ATTRIBUTES
118 For an explanation of the terms used in this section, see
119 .BR attributes (7).
120 .ad l
123 allbox;
124 lbx lb lb
125 l l l.
126 Interface       Attribute       Value
128 .BR strdup (),
129 .BR strndup (),
130 .BR strdupa (),
131 .BR strndupa ()
132 T}      Thread safety   MT-Safe
136 .sp 1
137 .SH CONFORMING TO
138 .\" 4.3BSD-Reno, not (first) 4.3BSD.
139 .BR strdup ()
140 conforms to SVr4, 4.3BSD, POSIX.1-2001.
141 .BR strndup ()
142 conforms to POSIX.1-2008.
143 .BR strdupa ()
145 .BR strndupa ()
146 are GNU extensions.
147 .SH SEE ALSO
148 .BR alloca (3),
149 .BR calloc (3),
150 .BR free (3),
151 .BR malloc (3),
152 .BR realloc (3),
153 .BR string (3),
154 .BR wcsdup (3)