]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strdup.3
tsearch.3: Minor tweak to Florian's patch
[thirdparty/man-pages.git] / man3 / strdup.3
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 2019-03-06 "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 .in -4n
46 Feature Test Macro Requirements for glibc (see
47 .BR feature_test_macros (7)):
48 .in
49 .PP
50 .PD 0
51 .ad l
52 .BR strdup ():
53 .RS 4
54 _XOPEN_SOURCE\ >=\ 500
55 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
56 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
57 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
58 .RE
59 .PP
60 .BR strndup ():
61 .RS 4
62 .TP 4
63 Since glibc 2.10:
64 _POSIX_C_SOURCE\ >=\ 200809L
65 .TP
66 Before glibc 2.10:
67 _GNU_SOURCE
68 .RE
69 .PP
70 .BR strdupa (),
71 .BR strndupa ():
72 _GNU_SOURCE
73 .ad
74 .PD
75 .SH DESCRIPTION
76 The
77 .BR strdup ()
78 function returns a pointer to a new string which
79 is a duplicate of the string
80 .IR s .
81 Memory for the new string is
82 obtained with
83 .BR malloc (3),
84 and can be freed with
85 .BR free (3).
86 .PP
87 The
88 .BR strndup ()
89 function is similar, but copies at most
90 .I n
91 bytes.
92 If
93 .I s
94 is longer than
95 .IR n ,
96 only
97 .I n
98 bytes are copied, and a terminating null byte (\(aq\e0\(aq) is added.
99 .PP
100 .BR strdupa ()
101 and
102 .BR strndupa ()
103 are similar, but use
104 .BR alloca (3)
105 to allocate the buffer.
106 They are available only when using the GNU
107 GCC suite, and suffer from the same limitations described in
108 .BR alloca (3).
109 .SH RETURN VALUE
110 On success, the
111 .BR strdup ()
112 function returns a pointer to the duplicated
113 string.
114 It returns NULL if insufficient memory was available, with
115 .I errno
116 set to indicate the cause of the error.
117 .SH ERRORS
118 .TP
119 .B ENOMEM
120 Insufficient memory available to allocate duplicate string.
121 .SH ATTRIBUTES
122 For an explanation of the terms used in this section, see
123 .BR attributes (7).
124 .TS
125 allbox;
126 lbw31 lb lb
127 l l l.
128 Interface Attribute Value
129 T{
130 .BR strdup (),
131 .BR strndup (),
132 .BR strdupa (),
133 .br
134 .BR strndupa ()
135 T} Thread safety MT-Safe
136 .TE
137 .sp 1
138 .SH CONFORMING TO
139 .\" 4.3BSD-Reno, not (first) 4.3BSD.
140 .BR strdup ()
141 conforms to SVr4, 4.3BSD, POSIX.1-2001.
142 .BR strndup ()
143 conforms to POSIX.1-2008.
144 .BR strdupa ()
145 and
146 .BR strndupa ()
147 are GNU extensions.
148 .SH SEE ALSO
149 .BR alloca (3),
150 .BR calloc (3),
151 .BR free (3),
152 .BR malloc (3),
153 .BR realloc (3),
154 .BR string (3),
155 .BR wcsdup (3)