]>
Commit | Line | Data |
---|---|---|
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 |
31 | strdup, 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 | |
46 | Feature 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 | |
63 | Since glibc 2.10: | |
98dbe7af | 64 | POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700 |
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 | |
1c44bd5b MK |
79 | is a duplicate of the string \fIs\fP. |
80 | Memory for the new string is | |
60a90ecd MK |
81 | obtained with |
82 | .BR malloc (3), | |
83 | and can be freed with | |
84 | .BR free (3). | |
fea681da | 85 | |
60a90ecd MK |
86 | The |
87 | .BR strndup () | |
88 | function is similar, but only copies at most | |
c13182ef MK |
89 | \fIn\fP characters. |
90 | If \fIs\fP is longer than \fIn\fP, only \fIn\fP | |
f81fb444 | 91 | characters are copied, and a terminating null byte (\(aq\\0\(aq) is added. |
fea681da | 92 | |
60a90ecd MK |
93 | .BR strdupa () |
94 | and | |
95 | .BR strndupa () | |
96 | are similar, but use | |
97 | .BR alloca (3) | |
c13182ef MK |
98 | to allocate the buffer. |
99 | They are only available when using the GNU | |
60a90ecd MK |
100 | GCC suite, and suffer from the same limitations described in |
101 | .BR alloca (3). | |
fea681da | 102 | .SH "RETURN VALUE" |
60a90ecd MK |
103 | The |
104 | .BR strdup () | |
105 | function returns a pointer to the duplicated | |
fea681da MK |
106 | string, or NULL if insufficient memory was available. |
107 | .SH ERRORS | |
108 | .TP | |
109 | .B ENOMEM | |
110 | Insufficient memory available to allocate duplicate string. | |
111 | .SH "CONFORMING TO" | |
b14d4aa5 | 112 | .\" 4.3BSD-Reno, not (first) 4.3BSD. |
68e1685c MK |
113 | .BR strdup () |
114 | conforms to SVr4, 4.3BSD, POSIX.1-2001. | |
60a90ecd MK |
115 | .BR strndup (), |
116 | .BR strdupa (), | |
117 | and | |
118 | .BR strndupa () | |
119 | are 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) |