]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strdup.3
man*/: ffix (un-bracket tables)
[thirdparty/man-pages.git] / man3 / strdup.3
1 '\" t
2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9 .\" 386BSD man pages
10 .\" Modified Sun Jul 25 10:41:34 1993 by Rik Faith (faith@cs.unc.edu)
11 .\" Modified Wed Oct 17 01:12:26 2001 by John Levon <moz@compsoc.man.ac.uk>
12 .TH strdup 3 (date) "Linux man-pages (unreleased)"
13 .SH NAME
14 strdup, strndup, strdupa, strndupa \- duplicate a string
15 .SH LIBRARY
16 Standard C library
17 .RI ( libc ", " \-lc )
18 .SH SYNOPSIS
19 .nf
20 .B #include <string.h>
21 .PP
22 .BI "char *strdup(const char *" s );
23 .PP
24 .BI "char *strndup(const char " s [. n "], size_t " n );
25 .BI "char *strdupa(const char *" s );
26 .BI "char *strndupa(const char " s [. n "], size_t " n );
27 .fi
28 .PP
29 .RS -4
30 Feature Test Macro Requirements for glibc (see
31 .BR feature_test_macros (7)):
32 .RE
33 .PP
34 .BR strdup ():
35 .nf
36 _XOPEN_SOURCE >= 500
37 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
38 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
39 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
40 .fi
41 .PP
42 .BR strndup ():
43 .nf
44 Since glibc 2.10:
45 _POSIX_C_SOURCE >= 200809L
46 Before glibc 2.10:
47 _GNU_SOURCE
48 .fi
49 .PP
50 .BR strdupa (),
51 .BR strndupa ():
52 .nf
53 _GNU_SOURCE
54 .fi
55 .SH DESCRIPTION
56 The
57 .BR strdup ()
58 function returns a pointer to a new string which
59 is a duplicate of the string
60 .IR s .
61 Memory for the new string is
62 obtained with
63 .BR malloc (3),
64 and can be freed with
65 .BR free (3).
66 .PP
67 The
68 .BR strndup ()
69 function is similar, but copies at most
70 .I n
71 bytes.
72 If
73 .I s
74 is longer than
75 .IR n ,
76 only
77 .I n
78 bytes are copied, and a terminating null byte (\[aq]\e0\[aq]) is added.
79 .PP
80 .BR strdupa ()
81 and
82 .BR strndupa ()
83 are similar, but use
84 .BR alloca (3)
85 to allocate the buffer.
86 .SH RETURN VALUE
87 On success, the
88 .BR strdup ()
89 function returns a pointer to the duplicated
90 string.
91 It returns NULL if insufficient memory was available, with
92 .I errno
93 set to indicate the error.
94 .SH ERRORS
95 .TP
96 .B ENOMEM
97 Insufficient memory available to allocate duplicate string.
98 .SH ATTRIBUTES
99 For an explanation of the terms used in this section, see
100 .BR attributes (7).
101 .TS
102 allbox;
103 lbx lb lb
104 l l l.
105 Interface Attribute Value
106 T{
107 .na
108 .nh
109 .BR strdup (),
110 .BR strndup (),
111 .BR strdupa (),
112 .BR strndupa ()
113 T} Thread safety MT-Safe
114 .TE
115 .sp 1
116 .SH STANDARDS
117 .TP
118 .BR strdup ()
119 .TQ
120 .BR strndup ()
121 POSIX.1-2008.
122 .TP
123 .BR strdupa ()
124 .TQ
125 .BR strndupa ()
126 GNU.
127 .SH HISTORY
128 .TP
129 .BR strdup ()
130 SVr4, 4.3BSD-Reno, POSIX.1-2001.
131 .TP
132 .BR strndup ()
133 POSIX.1-2008.
134 .TP
135 .BR strdupa ()
136 .TQ
137 .BR strndupa ()
138 GNU.
139 .SH SEE ALSO
140 .BR alloca (3),
141 .BR calloc (3),
142 .BR free (3),
143 .BR malloc (3),
144 .BR realloc (3),
145 .BR string (3),
146 .BR wcsdup (3)