]>
Commit | Line | Data |
---|---|---|
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> | |
4b8c67d9 | 31 | .TH STRDUP 3 2017-09-15 "GNU" "Linux Programmer's Manual" |
fea681da MK |
32 | .SH NAME |
33 | strdup, 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 |
46 | Feature 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 | |
63 | Since glibc 2.10: | |
b0da7b8b | 64 | _POSIX_C_SOURCE\ >=\ 200809L |
8a3a2952 MK |
65 | .TP |
66 | Before 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 |
76 | The |
77 | .BR strdup () | |
78 | function returns a pointer to a new string which | |
46d8df8e MK |
79 | is a duplicate of the string |
80 | .IR s . | |
1c44bd5b | 81 | Memory for the new string is |
60a90ecd MK |
82 | obtained with |
83 | .BR malloc (3), | |
84 | and can be freed with | |
85 | .BR free (3). | |
847e0d88 | 86 | .PP |
60a90ecd MK |
87 | The |
88 | .BR strndup () | |
33a0ccb2 | 89 | function is similar, but copies at most |
46d8df8e MK |
90 | .I n |
91 | bytes. | |
92 | If | |
93 | .I s | |
94 | is longer than | |
95 | .IR n , | |
96 | only | |
97 | .I n | |
a00b7454 | 98 | bytes are copied, and a terminating null byte (\(aq\\0\(aq) is added. |
847e0d88 | 99 | .PP |
60a90ecd MK |
100 | .BR strdupa () |
101 | and | |
102 | .BR strndupa () | |
103 | are similar, but use | |
104 | .BR alloca (3) | |
c13182ef | 105 | to allocate the buffer. |
33a0ccb2 | 106 | They are available only when using the GNU |
60a90ecd MK |
107 | GCC suite, and suffer from the same limitations described in |
108 | .BR alloca (3). | |
47297adb | 109 | .SH RETURN VALUE |
6a788310 | 110 | On success, the |
60a90ecd MK |
111 | .BR strdup () |
112 | function returns a pointer to the duplicated | |
6a788310 MK |
113 | string. |
114 | It returns NULL if insufficient memory was available, with | |
115 | .I errno | |
116 | set to indicate the cause of the error. | |
fea681da MK |
117 | .SH ERRORS |
118 | .TP | |
119 | .B ENOMEM | |
120 | Insufficient memory available to allocate duplicate string. | |
7ab7eb48 MS |
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 | |
847e0d88 | 137 | .sp 1 |
47297adb | 138 | .SH CONFORMING TO |
b14d4aa5 | 139 | .\" 4.3BSD-Reno, not (first) 4.3BSD. |
68e1685c MK |
140 | .BR strdup () |
141 | conforms to SVr4, 4.3BSD, POSIX.1-2001. | |
82f4ea01 MK |
142 | .BR strndup () |
143 | conforms to POSIX.1-2008. | |
144 | .BR strdupa () | |
60a90ecd MK |
145 | and |
146 | .BR strndupa () | |
147 | are 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) |